What are the steps to use JDBC to connect to a database in Java?Adoption Rate:46% Class 6 2015.06.30 1. Registration Driver
Class.forName ("Com.mysql.jdbc.Driver");//This is the driver that connects the MySQL database
2. Get a database connection
Java.sql.Connection conn=java.sql.drivermanager.getconnection (); 3. Get an expression
Java.sql.Statement stmt=conn.createstatement ("jdbc:mysql://localhost/test?useunicode=true&characterencoding =gbk "," root "," null ");//Three parameters are the URL of the database connection, user name, password 4. Execute SQL
Java.sql.ResultSet rs=stmt.executequery ("SELECT * from user"); 5. Display the data in the result set
while (Rs.next ()) {
System.out.println (Rs.getint (1));
System.out.println (rs.getstring ("username"));
System.out.println (rs.getstring ("password"));
System.out.pringln ();
}//executing an INSERT statement
Stmt.executeupdate ("INSERT into user values (1, ' Chinese ', ' 345 ')");
6. Releasing Resources
Rs.close ();
Stmt.close ();
Conn.close ();
To connect to a database using JDBC in Java