1. Commonly Used try { Class. forname ("com. MySQL. JDBC. Driver ") ; // Load the database driver string url = "JDBC: mysql: // localhost: 3306/databasename"; // connect the database to the sub-protocol connection conn = drivermanager. getconnection (URL, "username", "password"); statement stmt = Conn. createstatement (); resultset rs1_stmt.exe cutequery ("select * From tablename"); While (RS. next () {// continuously points to the next record system. out. println ("deptno:" + Rs. getint (1); system. out. println ("\ tdeptname:" + Rs. getstring (2); system. out. println ("\ tloc:" + Rs. getstring (3);} Rs. close (); stmt. close (); Conn. close ();} catch (classnotfoundexception e) {system. out. println ("the specified driver cannot be found Program Class! ");} Catch (sqlexception e) {e. printstacktrace ();} 2. Set try { System. setproperty ("JDBC. Driver", "com. MySQL. JDBC. Driver ") ; // System attribute specifies the database driver string url = "JDBC: mysql: // localhost: 3306/databasename"; // database connection sub-protocol connection conn = drivermanager. getconnection (URL, "username", "password"); statement stmt = Conn. createstatement (); resultset rs1_stmt.exe cutequery ("select * From tablename"); While (RS. next () {// continuously points to the next record system. out. println ("deptno:" + Rs. getint (1); system. out. println ("\ tdeptname:" + Rs. getstring (2); system. out. println ("\ tloc:" + Rs. getstring (3);} Rs. close (); stmt. close (); Conn. close ();} catch (sqlexception e) {e. printstacktrace ();}
3. An intuitive method to register the JDBC driver of the corresponding dB. 3. Import the corresponding lib during compilation. Try { New COM. MySQL. JDBC. Driver () ; // Create a driver object and load the database driver string url = "JDBC: mysql: // localhost: 3306/databasename"; // connect the database to the sub-protocol connection conn = drivermanager. getconnection (URL, "username", "password"); statement stmt = Conn. createstatement (); resultset rs1_stmt.exe cutequery ("select * From tablename"); While (RS. next () {// continuously points to the next record system. out. println ("deptno:" + Rs. getint (1); system. out. println ("\ tdeptname:" + Rs. getstring (2); system. out. println ("\ tloc:" + Rs. getstring (3);} Rs. close (); stmt. close (); Conn. close ();} catch (sqlexception e) {e. printstacktrace ();}