You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-dev@db.apache.org by Samuel Andrew McIntyre <fu...@nonintuitive.com> on 2005/01/24 10:53:17 UTC
[PATCH] allow demo compilation without db2jcc.jar
Attached is a patch to allow the network server demo to compile without
db2jcc.jar. With the patch, the demo can compile without db2jcc.jar
present, and the resulting demo classes will run when db2jcc.jar is
present in the classpath at runtime.
In NsSample.java,
- the connection URL is corrected so that there is a semicolon after
'create=true'
- references to DB2Connection are replaced with java.sql.Connection.
In SimpleNetworkClientSample.java
- the client's DataSource and its properties are accessed via
reflection to avoid referencing DB2SimpleDataSource directly.
- the connection is instantiated via reflection to avoid referencing
the DB2Connection class directly.
In NsSampleClientThread.java
- references to DB2Connection are replaced with java.sql.Connection
Please let me know if you have concerns regarding this patch. My only
concern at the moment is that the reuse of variable names may lead to
confusion when reading the sample code.
Thanks,
andrew
Re: [PATCH] allow demo compilation without db2jcc.jar
Posted by Samuel Andrew McIntyre <fu...@nonintuitive.com>.
On Jan 24, 2005, at 1:53 AM, Samuel Andrew McIntyre wrote:
> Attached is a patch to allow the network server demo to compile
> without db2jcc.jar.
Here's a revised patch to fix a typo in SimpleNetworkClientSample and
to use the DriverManager to get the connection instead of reflection.
andrew
-- patch included inline:
Index: java/demo/nserverdemo/NsSample.java
===================================================================
--- java/demo/nserverdemo/NsSample.java (revision 126352)
+++ java/demo/nserverdemo/NsSample.java (working copy)
@@ -12,6 +12,7 @@
import java.sql.SQLException;
import java.sql.DriverManager;
import java.io.IOException;
+import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
@@ -70,7 +71,7 @@
// To connect to Derby Network Server
// This URL describes the target database for type 4
connectivity
// Notice that the properties may be established via the URL
syntax
- private static final String CS_NS_DBURL=
"jdbc:derby:net://localhost:"+NETWORKSERVER_PORT+"/NSSampledb;
create=true:retrieveMessagesFromServerOnGetMessage=true;
deferPrepares=true;";
+ private static final String CS_NS_DBURL=
"jdbc:derby:net://localhost:"+NETWORKSERVER_PORT+"/NSSampledb;
create=true;retrieveMessagesFromServerOnGetMessage=true;
deferPrepares=true;";
public static void main(String[] args) throws Exception {
@@ -78,7 +79,7 @@
// DB2Connection provides additional functionality than
java.sql.Connection
// One can use either depending on the requirements
- com.ibm.db2.jcc.DB2Connection conn = null;
+ Connection conn = null;
PrintWriter pw = null;
@@ -149,7 +150,8 @@
// Get database connection using the JCC client via
DriverManager api
try {
- conn = (com.ibm.db2.jcc.DB2Connection)
DriverManager.getConnection(CS_NS_DBURL, properties);
+
+ conn = (Connection)
DriverManager.getConnection(CS_NS_DBURL, properties);
} catch(Exception e) {
pw.println("[NsSample] Connection request
unsuccessful, exception thrown was: ");
pw.println("[NsSample] Please check if all the
jar files are in the classpath and the dbUrl is set correctly.");
Index: java/demo/nserverdemo/SimpleNetworkClientSample.java
===================================================================
--- java/demo/nserverdemo/SimpleNetworkClientSample.java
(revision 126352)
+++ java/demo/nserverdemo/SimpleNetworkClientSample.java
(working copy)
@@ -7,6 +7,7 @@
*/
import java.sql.*;
+import java.lang.reflect.*;
import javax.sql.DataSource;
import java.util.Properties;
import java.io.BufferedReader;
@@ -49,9 +50,10 @@
private static int NETWORKSERVER_PORT=1527;
/**
- * DB2 JDBC UNIVERSAL DRIVER class name
+ * DB2 JDBC UNIVERSAL DRIVER class names
*/
private static final String DB2_JDBC_UNIVERSAL_DRIVER =
"com.ibm.db2.jcc.DB2Driver";
+ private static final String DB2_JCC_DS =
"com.ibm.db2.jcc.DB2SimpleDataSource";
/**
* This URL is used to connect to Derby Network Server using
the DriverManager.
@@ -138,27 +140,42 @@
* @throws Exception if there is any error
*/
public static javax.sql.DataSource getClientDataSource(String
database, String user, String
-
password) throws SQLException
+
password) throws SQLException, ClassNotFoundException,
InstantiationException, IllegalAccessException, NoSuchMethodException,
InvocationTargetException
{
+ Class nsDataSource = Class.forName(DB2_JCC_DS);
+ DataSource ds = (DataSource) nsDataSource.newInstance();
- com.ibm.db2.jcc.DB2SimpleDataSource ds = new
com.ibm.db2.jcc.DB2SimpleDataSource();
-
// can also include Derby URL attributes along with the
database name
- ds.setDatabaseName(database);
+ Class[] methodParams = new Class[] {String.class};
+ Method dbname =
nsDataSource.getMethod("setDatabaseName", methodParams);
+ Object[] args = new Object[] {database};
+ dbname.invoke(ds, args);
- if (user != null)
- ds.setUser(user);
- if (password != null)
- ds.setPassword(password);
-
+ if (user != null) {
+ Method setuser =
nsDataSource.getMethod("setUser", methodParams);
+ args = new Object[] {user};
+ setuser.invoke(ds, args);
+ }
+ if (password != null) {
+ Method setpw =
nsDataSource.getMethod("setPassword", methodParams);
+ args = new Object[] {password};
+ setpw.invoke(ds, args);
+ }
// host on which network server is running
- ds.setServerName("localhost");
+ Method servername =
nsDataSource.getMethod("setServerName", methodParams);
+ args = new Object[] {"localhost"};
+ servername.invoke(ds, args);
// port on which Network Server is listening
- ds.setPortNumber(1527);
+ methodParams = new Class[] {int.class};
+ Method portnumber =
nsDataSource.getMethod("setPortNumber", methodParams);
+ args = new Object[] {new Integer(1527)};
+ portnumber.invoke(ds, args);
// driver type must be 4 to access Derby Network Server
- ds.setDriverType(4);
+ Method drivertype =
nsDataSource.getMethod("setDriverType", methodParams);
+ args = new Object[] {new Integer(4)};
+ drivertype.invoke(ds, args);
return ds;
@@ -193,7 +210,7 @@
properties.setProperty("password","scape");
// Get database connection using the JCC client via
DriverManager api
- Connection conn = (com.ibm.db2.jcc.DB2Connection)
DriverManager.getConnection(CS_NS_DBURL, properties);
+ Connection conn =
DriverManager.getConnection(CS_NS_DBURL,properties);
return conn;
}
Index: java/demo/nserverdemo/NsSampleClientThread.java
===================================================================
--- java/demo/nserverdemo/NsSampleClientThread.java (revision
126352)
+++ java/demo/nserverdemo/NsSampleClientThread.java (working copy)
@@ -319,7 +319,7 @@
/**
* Create necessary schema if schema not already created
*/
- public static void
checkAndCreateSchema(com.ibm.db2.jcc.DB2Connection conn,PrintWriter pw)
{
+ public static void checkAndCreateSchema(Connection
conn,PrintWriter pw) {
Statement stmt = null;
ResultSet rs = null;
@@ -378,7 +378,7 @@
/**
* Loads schema , inserts 'rowsToInsert' number of rows into
the table
*/
- public static void loadSchema(com.ibm.db2.jcc.DB2Connection
conn,int rowsToInsert,PrintWriter pw) {
+ public static void loadSchema(Connection conn,int
rowsToInsert,PrintWriter pw) {
int insertsRemaining = rowsToInsert;
PreparedStatement ps=null;
--- and also attached: