1 21 22 import java.sql.*; 23 import javax.sql.DataSource ; 24 import org.apache.derby.drda.NetworkServerControl; 25 import java.util.Properties ; 26 import java.io.BufferedReader ; 27 import java.io.InputStreamReader ; 28 29 59 public class SimpleNetworkServerSample 60 { 61 62 66 private static String DBNAME="NSSimpleDB"; 67 68 69 public static void main (String [] args) 70 throws Exception 71 { 72 Connection embeddedConn = null; 73 74 try 75 { 76 startNetworkServer(); 77 78 85 86 } 87 catch (Exception e) 88 { 89 System.out.println("Failed to start NetworkServer: " + e); 90 System.exit(1); 91 } 92 93 try 94 { 95 embeddedConn = getEmbeddedConnection(DBNAME,"create=true;"); 102 System.out.println("Got an embedded connection."); 103 104 105 System.out.println("Testing embedded connection by executing a sample query "); 106 test(embeddedConn); 108 109 String howToConnect = ijUsage(); 111 System.out.println(howToConnect); 112 113 waitForExit(); 114 115 } 116 catch (SQLException sqle) 117 { 118 System.out.println("Failure making connection: " + sqle); 119 sqle.printStackTrace(); 120 } 121 finally 122 { 123 124 if(embeddedConn != null) 125 embeddedConn.close(); 126 try 127 { 128 DriverManager.getConnection("jdbc:derby:;shutdown=true"); 130 } 131 catch(SQLException se) 132 { 133 } 135 136 } 137 138 } 139 140 157 158 public static void startNetworkServer() throws Exception 159 { 160 startWithProperty(); 163 waitForStart(); 164 } 165 166 177 178 private static void startWithProperty() throws Exception 179 { 180 System.out.println("Starting Network Server"); 181 System.setProperty("derby.drda.startNetworkServer","true"); 182 183 Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance(); 185 } 186 187 188 189 196 private static void waitForStart() throws Exception 197 { 198 199 org.apache.derby.drda.NetworkServerControl server = null; 201 202 server = new NetworkServerControl(); 207 208 System.out.println("Testing if Network Server is up and running!"); 209 for (int i = 0; i < 10 ; i ++) 210 { 211 try { 212 213 Thread.currentThread().sleep(5000); 214 server.ping(); 215 } 216 catch (Exception e) 217 { 218 System.out.println("Try #" + i + " " +e.toString()); 219 if (i == 9 ) 220 { 221 System.out.println("Giving up trying to connect to Network Server!"); 222 throw e; 223 } 224 } 225 } 226 System.out.println("Derby Network Server now running"); 227 228 } 229 230 243 public static Connection getEmbeddedConnection(String database,String attributes) 244 throws Exception 245 { 246 String dbUrl = "jdbc:derby:"+database +";"+attributes; 247 Connection conn = DriverManager.getConnection(dbUrl); 248 return conn; 249 } 250 251 252 253 258 public static void test(Connection conn) 259 throws Exception 260 { 261 262 Statement stmt = null; 263 ResultSet rs = null; 264 try 265 { 266 stmt = conn.createStatement(); 268 rs = stmt.executeQuery("select count(*) from sys.systables"); 269 while(rs.next()) 270 System.out.println("number of rows in sys.systables = "+ rs.getInt(1)); 271 272 } 273 catch(SQLException sqle) 274 { 275 System.out.println("SQLException when querying on the database connection; "+ sqle); 276 throw sqle; 277 } 278 finally 279 { 280 if(rs != null) 281 rs.close(); 282 if(stmt != null) 283 stmt.close(); 284 } 285 286 } 287 288 289 295 private static void waitForExit() throws Exception 296 { 297 System.out.println("Clients can continue to connect: "); 298 BufferedReader in = 299 new BufferedReader (new InputStreamReader (System.in)); 300 System.out.println("Press [Enter] to stop Server"); 301 in.readLine(); 302 } 303 304 307 private static String ijUsage() 308 { 309 310 String ijUsage = "\nWhile my app is busy with embedded work, "; 311 ijUsage += "ij might connect like this:\n\n"; 312 ijUsage += "\t$ java -Dij.user=me -Dij.password=pw -Dij.protocol=jdbc:derby://localhost:1527/ org.apache.derby.tools.ij\n"; 313 ijUsage += "\tij> connect '" + DBNAME + "';\n\n"; 314 315 return ijUsage; 316 } 317 } 318 319 320 321 322 323 324 | Popular Tags |