JDBC Connection database, jdbc database
JDBC connection to the database is generally divided into three steps:
(1) register the database driver
(2) create a database connection URL
(3) obtain the Connection object
The Code is as follows:
1 try {2 // load the database driver and register it with the driver Manager 3 Class. forName ("com. mysql. jdbc. driver "); 4 // database connection String 5 String url =" jdbc: mysql: // localhost: 3306/mysqltest "; 6 // database userName 7 String userName = "root"; 8 // Database passWord 9 String passWord = "123456"; 10 // create Connection 11 Connection conn = (Connection) driverManager. getConnection (url, userName, passWord); 12 // determine whether the database connection is null 13 if (conn! = Null) {14 // output connection information 15 out. print ("database connection successful! "); 16 // close database connection 17 conn. close (); 18} else {19 // output connection error message 20 out. println (" database connection failed! "); 21} 22} catch (ClassNotFoundException e) {23 e. printStackTrace (); 24}
View Code