1 21 22 import java.lang.reflect.InvocationTargetException ; 23 import java.lang.reflect.Method ; 24 import java.sql.Connection ; 25 import java.sql.DriverManager ; 26 import java.sql.ResultSet ; 27 import java.sql.SQLException ; 28 import java.sql.Statement ; 29 import java.util.Properties ; 30 31 import javax.sql.DataSource ; 32 33 52 public class SimpleNetworkClientSample 53 { 54 55 59 private static String DBNAME="NSSampleDB"; 60 61 64 private static int NETWORKSERVER_PORT=1527; 65 66 69 private static final String DB2_JDBC_UNIVERSAL_DRIVER = "com.ibm.db2.jcc.DB2Driver"; 70 private static final String DB2_JCC_DS = "com.ibm.db2.jcc.DB2SimpleDataSource"; 71 74 75 public static final String DERBY_CLIENT_DRIVER = "org.apache.derby.jdbc.ClientDriver"; 76 private static final String DERBY_CLIENT_DS = "org.apache.derby.jdbc.ClientDataSource"; 77 78 83 private static final String CS_NS_DBURL= "jdbc:derby:net://localhost:"+NETWORKSERVER_PORT+"/"+DBNAME+";retrieveMessagesFromServerOnGetMessage=true;deferPrepares=true;"; 84 85 private static final String DERBY_CLIENT_URL= "jdbc:derby://localhost:"+ NETWORKSERVER_PORT+"/"+DBNAME+";create=true"; 87 88 String url = DERBY_CLIENT_URL; 90 String jdbcDriver = DERBY_CLIENT_DRIVER; 91 String jdbcDataSource = DERBY_CLIENT_DS; 92 93 public static void main (String [] args) throws Exception 94 { 95 96 new SimpleNetworkClientSample().startSample(args); 97 98 } 99 public void startSample (String [] args) throws Exception 100 { 101 DataSource clientDataSource = null; 102 Connection clientConn1 = null; 103 Connection clientConn2 = null; 104 105 106 try 107 { 108 System.out.println("Starting Sample client program "); 109 parseArguments(args); 111 112 loadDriver(); 114 115 clientConn1 = getClientDriverManagerConnection(); 117 System.out.println("Got a client connection via the DriverManager."); 118 119 javax.sql.DataSource myDataSource = getClientDataSource(DBNAME, null, null); 121 122 clientConn2 = getClientDataSourceConn(myDataSource); 124 System.out.println("Got a client connection via a DataSource."); 125 126 System.out.println("Testing the connection obtained via DriverManager by executing a sample query "); 128 test(clientConn1); 129 System.out.println("Testing the connection obtained via a DataSource by executing a sample query "); 130 test(clientConn2); 131 132 System.out.println("Goodbye!"); 133 } 134 catch (SQLException sqle) 135 { 136 System.out.println("Failure making connection: " + sqle); 137 sqle.printStackTrace(); 138 } 139 finally 140 { 141 142 if(clientConn1 != null) 143 clientConn1.close(); 144 if(clientConn2 != null) 145 clientConn2.close(); 146 } 147 } 148 149 156 public Connection getClientDataSourceConn(javax.sql.DataSource ds) 157 throws Exception 158 { 159 Connection conn = ds.getConnection("usr2", "pass2"); 160 System.out.print("connection from datasource; getDriverName = "); 161 System.out.println(conn.getMetaData().getDriverName()); 162 return conn; 163 } 164 165 175 public javax.sql.DataSource getClientDataSource(String database, String user, String 176 password) throws SQLException , ClassNotFoundException , InstantiationException , IllegalAccessException , NoSuchMethodException , InvocationTargetException 177 { 178 Class nsDataSource = Class.forName(jdbcDataSource); 179 DataSource ds = (DataSource ) nsDataSource.newInstance(); 180 181 Class [] methodParams = new Class [] {String .class}; 183 Method dbname = nsDataSource.getMethod("setDatabaseName", methodParams); 184 Object [] args = new Object [] {database}; 185 dbname.invoke(ds, args); 186 187 if (user != null) { 188 Method setuser = nsDataSource.getMethod("setUser", methodParams); 189 args = new Object [] {user}; 190 setuser.invoke(ds, args); 191 } 192 if (password != null) { 193 Method setpw = nsDataSource.getMethod("setPassword", methodParams); 194 args = new Object [] {password}; 195 setpw.invoke(ds, args); 196 } 197 Method servername = nsDataSource.getMethod("setServerName", methodParams); 199 args = new Object [] {"localhost"}; 200 servername.invoke(ds, args); 201 202 methodParams = new Class [] {int.class}; 204 Method portnumber = nsDataSource.getMethod("setPortNumber", methodParams); 205 args = new Object [] {new Integer (1527)}; 206 portnumber.invoke(ds, args); 207 208 if(jdbcDataSource.equals( DB2_JCC_DS)) 210 { 211 Method drivertype = nsDataSource.getMethod("setDriverType", methodParams); 213 args = new Object [] {new Integer (4)}; 214 drivertype.invoke(ds, args); 215 } 216 217 return ds; 218 219 } 220 221 222 225 public void loadDriver() 226 throws Exception 227 { 228 Class.forName(jdbcDriver).newInstance(); 230 } 231 232 237 public Connection getClientDriverManagerConnection() 238 throws Exception 239 { 240 241 Properties properties = new java.util.Properties (); 244 245 properties.setProperty("user","cloud"); 247 properties.setProperty("password","scape"); 248 249 Connection conn = DriverManager.getConnection(url,properties); 251 252 return conn; 253 } 254 255 256 261 public void test(Connection conn) 262 throws Exception 263 { 264 265 Statement stmt = null; 266 ResultSet rs = null; 267 try 268 { 269 stmt = conn.createStatement(); 271 rs = stmt.executeQuery("select count(*) from sys.systables"); 272 while(rs.next()) 273 System.out.println("number of rows in sys.systables = "+ rs.getInt(1)); 274 275 } 276 catch(SQLException sqle) 277 { 278 System.out.println("SQLException when querying on the database connection; "+ sqle); 279 throw sqle; 280 } 281 finally 282 { 283 if(rs != null) 284 rs.close(); 285 if(stmt != null) 286 stmt.close(); 287 } 288 } 289 298 private void parseArguments(String [] args) 299 { 300 int length = args.length; 301 302 for (int index = 0; index < length; index++) 303 { 304 if (args[index].equalsIgnoreCase("jccjdbcclient")) 305 { 306 307 jdbcDriver = DB2_JDBC_UNIVERSAL_DRIVER; 308 jdbcDataSource = DB2_JCC_DS; 309 url = CS_NS_DBURL; 310 break; 311 } else if (args[index].equalsIgnoreCase("derbyClient")) 312 { 313 jdbcDriver = DERBY_CLIENT_DRIVER; 314 jdbcDataSource = DERBY_CLIENT_DS; 315 url = DERBY_CLIENT_URL; 316 break; 317 } 318 } 319 } 320 321 } 322 323 324 325 326 327 328 | Popular Tags |