Three ways to register JDBC

Source: Internet
Author: User

1. class.forname ("Com.mysql.jdbc.Driver");//Load Database driver

Java code

    1. Class.forName ("Com.mysql.jdbc.Driver"); //Load Database driver

    2. String url="Jdbc:mysql://localhost:3306/databasename";//Database Connection sub-protocol

    3. Connection conn=drivermanager.getconnection (URL,"username","password");

This is because the parameter is a string, so it is easy to modify and highly portable.

The most common way to register is also recommended.

2. new Com.mysql.jdbc.Driver () ;//Create driver object, load database driver

Java code

    1. new com.mysql.jdbc.Driver (); //Create driver object, load database driver

    2. String url="Jdbc:mysql://localhost:3306/databasename";//Database Connection sub-protocol

    3. Connection conn=drivermanager.getconnection (URL,"username","password");

This is not required to write Drivermanager.registerdriver (new Com.mysql.jdbc.Driver ()), because The static code of the Com.mysql.jdbc.Driver class is fast inside the operation that has been modified

Java code

    1. Static {

    2. Try {

    3. Java.sql.DriverManager.registerDriver (new Driver ());

    4. } Catch (SQLException E) {

    5. Throw New RuntimeException ("Can ' t register driver!");

    6. }

    7. }

by New Com.mysql.jdbc.Driver (), you need to create an instance of a class here. Creating an instance of the class requires importing the class through import in a Java file, otherwise it will be an error, in this way, the program cannot be compiled without leaving the driver class package, which can cause problems for the program to switch to other databases .


3.system.setproperty ("Jdbc.drivers", "Com.mysql.jdbc.Driver");

Java code

    1. System.setproperty ("Jdbc.driver","Com.mysql.jdbc.Driver"); //System Properties Specify database driver

    2. String url="Jdbc:mysql://localhost:3306/databasename";//Database Connection sub-protocol

    3. Connection conn=drivermanager.getconnection (URL,"username","password");

multiple JDBC drivers can be imported at the same time, separated by a colon ":"

such as System.setproperty ("Jdbc.drivers", "XXXDriver:XXXDriver:XXXDriver");

This will register three database drivers at a time.


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.