1 21 22 package nserverdemo; 23 24 25 import java.util.Properties ; 26 import java.sql.SQLException ; 27 import java.sql.DriverManager ; 28 import java.io.IOException ; 29 import java.sql.Connection ; 30 import java.sql.Statement ; 31 import java.sql.ResultSet ; 32 import java.sql.SQLException ; 33 import java.sql.SQLWarning ; 34 import java.io.PrintWriter ; 35 36 37 66 67 public class NsSample { 68 69 public static final String DB2_JDBC_UNIVERSAL_DRIVER = new String ("com.ibm.db2.jcc.DB2Driver"); 70 public static final String DERBY_CLIENT_DRIVER = "org.apache.derby.jdbc.ClientDriver"; 71 public static int NUM_ROWS = 50; 72 public static int ITERATIONS = 10; public static int NUM_CLIENT_THREADS = 2; 74 75 76 private static int NETWORKSERVER_PORT=1621; 78 79 public static final String CS_EMBED_DBURL="jdbc:derby:NSSampledb;"; 81 82 private static final String CS_NS_DBURL= "jdbc:derby:net://localhost:"+NETWORKSERVER_PORT+"/NSSampledb;create=true;retrieveMessagesFromServerOnGetMessage=true;deferPrepares=true;"; 86 private static final String DERBY_CLIENT_URL= "jdbc:derby://localhost:"+NETWORKSERVER_PORT+"/NSSampledb;create=true;"; 88 89 String url = DERBY_CLIENT_URL; 91 String jdbcDriver = DERBY_CLIENT_DRIVER; 92 93 public static void main(String [] args) throws Exception { 94 95 new nserverdemo.NsSample().startSample(args); 96 } 97 public void startSample(String [] args) throws Exception { 98 NetworkServerUtil nwServer; 99 100 Connection conn = null; 101 102 PrintWriter pw = null; 103 104 105 106 try { 107 108 parseArguments(args); 110 111 pw = new PrintWriter (System.out,true); pw.println("Using JDBC driver: " + jdbcDriver); 113 114 123 124 { 125 nwServer = new NetworkServerUtil(NETWORKSERVER_PORT,pw); 126 nwServer.start(); 127 128 boolean knowIfServerUp = false; int numTimes = 5; 130 131 while(!knowIfServerUp && (numTimes >0)) { 133 try { 134 numTimes--; 137 nwServer.testForConnection(); 138 knowIfServerUp = true; 139 } 140 catch(Exception e) { 141 System.out.println("[NsSample] Unable to obtain a connection to network server, trying again after 3000 ms."); 142 Thread.currentThread().sleep(3000); 143 } 144 } 145 if(!knowIfServerUp) { 146 pw.println("[NsSample] Exiting, since unable to connect to Derby Network Server."); 147 pw.println("[NsSample] Please try to increase the amount of time to keep trying to connect to the Server."); 148 System.exit(1); 149 } 150 151 pw.println("[NsSample] Derby Network Server started."); 152 } 153 154 155 pw.println("[NsSample] Sample Derby Network Server program demo starting. "); 156 pw.println("Please wait ....................."); 157 158 try { 160 Class.forName(jdbcDriver).newInstance(); 161 } catch (Exception e) { 162 pw.println("[NsSample] Unable to load the JDBC driver. Following exception was thrown"); 163 e.printStackTrace(); 164 System.exit(1); } 166 167 168 Properties properties = new java.util.Properties (); 171 172 properties.setProperty("user","cloud"); 174 properties.setProperty("password","scape"); 175 176 try { 178 179 conn = (Connection ) DriverManager.getConnection(url, properties); 180 } catch(Exception e) { 181 pw.println("[NsSample] Connection request unsuccessful, exception thrown was: "); 182 pw.println("[NsSample] Please check if all the jar files are in the classpath and the dbUrl is set correctly."); 183 e.printStackTrace(); 184 System.exit(1); } 186 187 NsSampleWork.checkAndCreateSchema(conn,pw); NsSampleWork.loadSchema(conn,NUM_ROWS,pw); conn.close(); 190 191 NsSampleClientThread clientThreads[] = new NsSampleClientThread[NUM_CLIENT_THREADS]; 193 194 195 201 clientThreads[0] = new NsSampleClientThread(1,CS_EMBED_DBURL,properties,pw); 202 clientThreads[0].start(); 203 204 205 210 for (int i=1; i<NUM_CLIENT_THREADS; i++) { 211 clientThreads[i] = new NsSampleClientThread(i+1,url,properties,pw); 212 clientThreads[i].start(); 213 214 } 215 216 for (int i = 0; i < NUM_CLIENT_THREADS; i++) 218 clientThreads[i].join(); 219 220 pw.println("[NsSample] Shutting down network server."); 222 nwServer.shutdown(); 223 pw.println("[NsSample] End of Network server demo."); 224 225 } catch (Exception e) { 226 e.printStackTrace(); 227 } 228 finally 229 { 230 if(pw != null) pw.close(); 231 } 232 } 233 234 243 private void parseArguments(String [] args) 244 { 245 int length = args.length; 246 247 for (int index = 0; index < length; index++) 248 { 249 if (args[index].equalsIgnoreCase("jccjdbcclient")) 250 { 251 jdbcDriver = DB2_JDBC_UNIVERSAL_DRIVER; 252 url = CS_NS_DBURL; 253 break; 254 } else if (args[index].equalsIgnoreCase("derbyClient")) 255 { 256 jdbcDriver = DERBY_CLIENT_DRIVER; 257 url = DERBY_CLIENT_URL; 258 break; 259 } 260 } 261 } 262 263 } 264 | Popular Tags |