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 is supported.
The native protocol is activated with an alternative URL specification
Connection con = DriverManager.getConnection("cegojdbcnative:geek:2200:TS1", "john", "miller");
|
For user authorization, a valid username and password must be given in both cases.
11.3 Using statements
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
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);
}
}
|
11.5 Using callable statements
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) );
}
|