Remote web server - Chapter 17 . ODBC/JDBC 519 You can save
Chapter 17 . ODBC/JDBC 519 You can save this connection as a program named FirstConnection.java and compile it by typing javac FirstConnection.java Once the program is compiled, you can run it by typing java FirstConnection When your first connection is successful, a Connection Successful message appears. Examining some of the code snippets reveals that a string variable, url, is initialized and contains the connection information for the server (localhost) that I want to connect to, along with the database (ecommerce) that I want to use. If I want to specify a port other than the default 3306, I can use localhost:port. For example, I could use localhost:5150like this: String url = jdbc:mysql://localhost/ecommerce ; A connection object, con, is created next: Connection con; The MySQL JDBC driver is now loaded, according to the syntax discussed in the previous section. If the loading of the driver is unsuccessful, an error message is printed; the following code displays the error message: try { Class.forName( org.gjt.mm.mysql.Driver ); } catch(java.lang.ClassNotFoundException e) { System.err.print( ClassNotFoundException: ); System.err.println(e.getMessage()); } The connection is attempted next; if it does not succeed, use SQLException to retrieve the error message. That operation looks like this: try { con = DriverManager.getConnection(url, suehring , evh5150 ); System.out.println( Connection established! ); con.close(); } catch(SQLException ex) { System.err.println( SQLException: + ex.getMessage()); } }
Go visit our java server pages services for a reliable, lowcost webhost to satisfy all your needs.