× News Cego SysMT Croom Web Statistics Impressum
SysMT Logo
11   Using the JDBC Driver
Back to TOC

From the client side, a common access interface used by Java applications is the JDBC ( Java Database Connection ) interface. Cego also provides access to the database via JDBC by using the available driver. The driver is provided as a jar file and must be included into the Java classpath.

( The cego JDBC driver package can be downloaded as opensource from www.lemke-it.com )

11.1   Loading the JDBC driver

Generally, a JDBC database driver is loaded dynamically in the main() routine ( or anywhere else ) before creating and instances of the java.sql API.

try {

   Class.forName( "de.lemkeit.cegojdbc.CegoDriver" ) ;

}
catch (ClassNotFoundException e)
{
   // handle here exception
   e.printStackTrace();
}

To load the Cego JDBC driver, yout need to load the class de.lemkeit.cegojdbc.CegoDriver as shown in the sample code above. If load is successful, you can use the driver in a common way.

11.2   JDBC connection String

With the connection string, the target database tableset is specified, the user wants to connect to. The string contains several parts, separated by a colon sign (:). As first, the name of the protocol is given. Actually this is a fixed value and must be cegojdbc. Next the name of the database host is given, a cego db daemon is running on. Depending on th configured port, the port number must be set next. As last, a valid tableset identifier must be given, which has been started up on the server side.

Connection con = DriverManager.getConnection("cegojdbc:geek:2200:TS1", "john", "miller");

Originally, the driver used the JDOM library for XML parsing and generation. From cegojdbc version 1.3.3, an additional native XML protocol was added. From driver version 1.4.0 another more faster serial protocol has been added which still transfers the data in readable form. From driver version 1.9.0 the fastserial protocol is supported, which is binary oriented. Actually, the following protocols are available, which can be selected in the URL specification

It is recommended to use the fastserial or serial protocol. For debugging reasons, one of the XML based protocols also can be selected.
Please note that the protocol configuration for the database server must match with the selected protocol

The corresponding protocol is activated with an alternative URL specification

Connection con = DriverManager.getConnection("cegojdbcfast:geek:2200:TS1", "john", "miller");

For user authorization, a valid username and password must be given in both cases.

11.3   Using statements

Any static SQL statement can be created with the connection handle by calling the createStatement method. The statement than can be used to execute several modifying SQL commands. The following code sample illustrates this

Statement stmt = con.createStatement();
stmt.execute("create table mytab (a int, b string(30)");
stmt.execute("insert into mytab values (1, 'hello1')");
stmt.execute("insert into mytab values (2, 'hello2')");
stmt.execute("insert into mytab values (3, 'hello3')");
...

11.4   Using prepared statements

With a prepared statement, a statement is created with dynamic content. The dynamic part is marked with question marks and can be setup later with appropriate methods.

PreparedStatement stmt = con.prepareStatement("select a, b from t1 where a > ?;");
stmt.setInt(1, 10);
ResultSet rs = stmt.executeQuery();

while ( rs.next() )
{
   String s1 = rs.getString(2);
   if ( rs.wasNull() )
   {
      System.out.println("NULL");
   }
   else
   {
      System.out.println(s1);
   }
}

In the sample above, a statement is created with one dynamic parameter. After the statement has been created using the prepareStatment method, the parameter is setup using the setInt method, where the first parameter indicated the position and the second parameter indicated the value for the parameter.

11.5   Using callable statements

Callable statements are normally used for calling stored procedures.Input and output parameters can be setup and the results can be retrieved after the procedure has been executed.

CallableStatement s1 = conn.prepareCall("{call myproc(?, ?, ?);}");
s1.setInt(1, 5);
s1.setString(2, "Huhu");
s1.registerOutParameter(3, java.sql.Types.VARCHAR);
s1.executeUpdate();
System.out.println("OutParam = " + s1.getString(3) );
CallableStatement s1 = conn.prepareCall("{? = call myfunc(?, ?)}");
s1.setInt(1, 5);
s1.setString(2, "foo");

for ( int i=0 ; i< 10 ; i++)
{
   s1.executeUpdate();
   System.out.println("OutParam = " + s1.getString(0) );
}

For a more detailed descrption of all avaiable JDBC classes and methods, please see the official product documentation available from Oracle™