This post continues the Java Integration Course. See the introduction post for an overview as well as instructions for setting up the database. In this post we add a very simple Java backend service which will be used by the JavaScript client in the subsequent development steps. Take a look at the Java code below:
<br />
package com.tmc.blog;</p>
<p>import java.sql.*;</p>
<p>public class Phonebook<br />
{<br />
/*<br />
The connectionURL consists of 3 to 4 parts, depending on if you have<br />
permissions set up for your mySQL database.</p>
<p> Part 1: "jdbc:mysql://localhost" refers to the url of your mySQL server.<br />
Part 2: "3306" refers to the port that mySQL server is running on.<br />
3306 is the default port for mySQL.<br />
Part 3: "phonebook" this is the name of the database that we will be<br />
interacting with.<br />
Part 4: "?username=blah&password=blah" - Optional.<br />
This query string is used for passing the username and password<br />
*/<br />
private String connectionURL = "jdbc:mysql://localhost:3306/phonebook?user=phonebookuser&password=password";<br />
private Connection conn;<br />
private Statement statement;</p>
<p> public Phonebook()<br />
{<br />
try<br />
{<br />
Class.forName( "com.mysql.jdbc.Driver" ).newInstance();<br />
this.conn = DriverManager.getConnection( connectionURL );<br />
this.statement = conn.createStatement();<br />
}<br />
catch( Exception ex )<br />
{<br />
ex.printStackTrace();<br />
}<br />
}</p>
<p> public ResultSet getListings() throws SQLException<br />
{<br />
try<br />
{<br />
String sql = "SELECT id, name, phonenumber FROM listing";<br />
ResultSet listings = statement.executeQuery( sql );</p>
<p> return listings;<br />
}<br />
catch( SQLException ex )<br />
{<br />
// handle any errors<br />
System.out.println( "SQLException: " + ex.getMessage() );<br />
System.out.println( "SQLState: " + ex.getSQLState() );<br />
System.out.println( "VendorError: " + ex.getErrorCode() );</p>
<p> throw ex;<br />
}<br />
}</p>
<p> public void createListing( String name, String phonenumber ) throws Exception<br />
{<br />
try<br />
{<br />
String sql = "insert into listing set name = ?, phonenumber = ?";<br />
PreparedStatement statement = conn.prepareStatement( sql );<br />
statement.setString( 1, name <a href='http://1buycialisonline.org/' title='buy cialis'>buy cialis</a> );<br />
statement.setString( 2, phonenumber );<br />
statement.execute();<br />
}<br />
catch( SQLException exception )<br />
{<br />
exception.printStackTrace();<br />
throw exception;<br />
}<br />
}<br />
}<br />
At this point the service can do the following:
- Connecting to the database – a connection is created in the class constructor.
- Retrieving all the records from the listing table (the getListings method).
- Creating a new record in the listing table.
The code assumes you created the user with the “phonebookuser” name and assigned “password” as the user’s password.
Over the course of the subsequent posts in this series, the code will get enhanced and expanded with additional functionality, but at this point, we will keep it as simple as possible until we get some basic integration in place. The next post will focus on configuring IDEA and Eclipse to compile and deploy the service into WebORB as well as validating that the service works.
