Database Access Wrapper In Java

Recently started doing some homework for java, I started with few simple concepts like JSP and servlets,

Having experience in PHP the web concepts are clear. Now its time to check whether the same concepts can work in JSP and Servlets or not

I started with simple JSP and Servlet Application for a common functionality forums.

(Github repo is created will commit on regular basis as i move ahead )

I first decided to do one db access class, that will hold the connection and some common functions so each time i dont need to write those repeat lines for open connection and close.

I did some R n D and come up with the following file

DBAccess.java

And sample implementation can be found here

CommonFunctions.java

 

I know I am somewhat fresher to this concept but trying to improve my knowledge in the concepts.

 

If you have any better ideas then please do share with me by putting comments or by dropping the email at abhijeetgk@gmail.com

 

Advertisements

How to run PHP on Google APP Engine

To have php run on GAE you need Quercus, though the name appears some difficult the process is much simple than expected, you just need to add few jar files change some settings and thats it..you will have your php code running on google app engine

The biggest problem during the whole process is to get the exact jar files

Google helped me find a SVN repo out there that had the 3 jars I needed, and  after loading them into the correct folder. Eclipse stopped throwing errors.  If you’re looking for the files I used, you can find them here:http://lportal.svn.sourceforge.net/viewvc/lportal/portal/trunk/lib/development/.  I determined that quercus.jar, resin_util.jar, and script-10.jar were needed for Quercus to do its thing correctly… at least that’s what I think.  Drop them in the WEB_INF/lib folder and you should be all set.

Now the actual process starts.

Step -1 Edit Web.xml file to have following lines of code

<servlet>
<servlet-name>Quercus Servlet</servlet-name>
<servlet-class>com.caucho.quercus.servlet.QuercusServlet</servlet-class>
<init-param>
<param-name>ini-file</param-name>
<param-value>WEB-INF/php.ini</param-value>
</init-param>
</servlet>

and

<servlet-mapping>
<servlet-name>Quercus Servlet</servlet-name>
<url-pattern>*.php</url-pattern>
</servlet-mapping>

I don’t know if any order is important here, but I put the php servlet first, followed by the default servlet, followed by the php servlet-mapping, followed by the default servlet-mapping.

Next on the list is appengine-web.xml. Add the following code right after closing of system-properties tag

<static-files>
<exclude path="/*.php" />
</static-files>
<resource-files>
<include path="/**.php" />
</resource-files>

Next to create one php.ini file in war/WEB-INF folder. Currently i kept it blank.

Now the final step is to create some php files to check. I created simple file that echo phpinfo to check php running fine at location war/index.php

Initially when I tested this out I was getting a strange error about org.mortbay.util.ajax.Continuation not found.  I don’t know what that is, but its included in the jetty-util-6.1.0.jar file, which I found here.  I’m unsure if this is specific to the development platform or required by Quercus

Now that’s it  I included that jar and my php started working on local instance of GAE application

I deployed on app engine, and there too it started smoothly.

Live Example

Github Hosted Project

Getting Started with Google Cloud SQL

After long wait finally i got the google cloud sql account and developed the sample app with that

Want to share a few concepts that might be helpful for beginners

First thing you need is google cloud sql account You can apply for limited preview HERE.

Once your account is approved (within 4-5 days). you will receive an activation email to activate the account. Activate with necessary details and in API console you will find the fresh new google cloud sql service is waiting for you.

The first thing you need to do is create a new instance to access the cloud services

Fill in the necessary details along with authorized app id

App id is you Google App Engine (GAE) application id, If you dont have one you can create it HERE.

Once your instance is ready you can see the same in Google API console to manage the details

Now you can proceed with actual development. I started with Java.

You can use the google cloud sql with GAE app.

To start developing GAE app I preferred the ECLIPSE and you need the google plugin. Will explain how to create the project for google app engine in later posts.

To connect to google cloud you need to import the library

com.google.appengine.api.rdbms.AppEngineDriver

After that in JDBC just change the connection string to following

Connection c = null;
c = DriverManager.getConnection("jdbc:google:rdbms://google_cloud_instance_name/database_name");

and thats it.. you are done. Proceed with rest things as regular JDBC to connect to cloud sql.