A JDBC Connection database is generally divided into 3 steps:
(1) Register database driver
(2) Creating a database connection URL
(3) Get Connection Connection object
These three steps, the specific code implementation is as follows:
1 Try{2 //Load Database driver, register to drive manager3Class.forName ("Com.mysql.jdbc.Driver");4 //Database connection String5String url = "Jdbc:mysql://localhost:3306/mysqltest";6 //Database user name7String userName = "root";8 //Database Password9String PassWord = "123456";Ten //Create a connection connection OneConnection conn =(Connection) drivermanager.getconnection (Url,username,password); A //determine if the database connection is empty - if(Conn! =NULL){ - //Output Connection Information theOut.print ("Database connection succeeded! "); - //To close a database connection - conn.close (); -}Else{ + //Output connection error message -OUT.PRINTLN ("Database connection failed! "); + } A}Catch(ClassNotFoundException e) { at e.printstacktrace (); -}
View Code
JDBC Connection Database