1. Three methods for registering a drive
(1) Class. forName ("com. mysql. jdbc. Driver ");
(2) System. setProperty ("jdbc. drivers", "com. mysql. jdbc. Driver ");
(3) DriverManager. registerDrivers (new com. mysql. jdbc. Driver () is not recommended ());
3. Three methods to obtain the connection
(1) Connection conn = DriverManager. getConnection ("jdbc: mysql: // localhost: 3306/test", "root", "123456 ");
(2) Connection conn = DriverManager. getConnection ("jdbc: mysql: // localhost: 3306/test? User = root & password = 123456 ");
(3) Connection conn = DriverManager. getConnection (strUrl, props );
Database. properties configuration file
Jdbc. drivers = com. mysql. jdbc. Driver
Jdbc. url = jdbc: mysql: // localhost: 3306/test
Jdbc. user = root
Jdbc. password = 123456
...
Properties props = new Properties ();
FileInputStream in = new FileInputStream ("database. properties"); // Obtain the database. properties configuration file
Props. load (in );
In. close ();
String strDriver = props. getProperty ("jdbc. drivers ");
String strUrl = props. getProperty ("jdbc. url ");
System. setProperty ("jdbc. drivers", strDriver );
Connection conn = DriverManager. getConnection (strUrl, props );
Register the drive and obtain the connection