Three methods for registering JDBC drivers

Source: Internet
Author: User
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 ();}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.