1. class.forname ("Com.mysql.jdbc.Driver");//Load Database driver
Java code
Class.forName ("Com.mysql.jdbc.Driver"); //Load Database driver
String url="Jdbc:mysql://localhost:3306/databasename";//Database Connection sub-protocol
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
new com.mysql.jdbc.Driver (); //Create driver object, load database driver
String url="Jdbc:mysql://localhost:3306/databasename";//Database Connection sub-protocol
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
Static {
Try {
Java.sql.DriverManager.registerDriver (new Driver ());
} Catch (SQLException E) {
Throw New RuntimeException ("Can ' t register driver!");
}
}
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
System.setproperty ("Jdbc.driver","Com.mysql.jdbc.Driver"); //System Properties Specify database driver
String url="Jdbc:mysql://localhost:3306/databasename";//Database Connection sub-protocol
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.