JDBC-java database connection steps, jdbc-java Database
Java database connection procedure
1. Load the driver
Class. forName ("com. mysql. java. Driver ");
2. Create a connection object
DriverManager class method:
Connection DriverManager. getConnection (String url); or Connection DriverManager. getConnection (String url, String user, String password );
Instance:
Connection conn = DriverManager. getConnection ("jdbc: mysql: // localhost: 3306/student? User = root & password = & characterEncoding = UTF-8 ");
Or
Connection conn = DriverManager. getConnection ("jdbc: mysql: // localhost: 3306/student", "root ","");
3. Create an SQL Execution object
Connection interface method:
Statement createStatement (); or PreparedStatement prepareStatement (String SQL );
Instance:
Statement stmt = conn. createStatement ();
Or
PreparedStatement ps = connn. prepareStatement ("select * from studentInfo ");
4. Create a ResultSet result set object
Statement interface method:
ResultSet executeQuery (String SQL );
Int executeUpdate (String SQL );
ResultSet getResult ();
PreparedStatement interface method:
Void setXXX (int paramIndes, XXX value); // XXX indicates the data type, such as int or String
Instance:
ResultSet rs = stmt.exe cuteQuery ("select * from studentInfo"); // return the query result set
Int num = stmt.exe cuteUpdate ("delete from studentInfo where name = 'hangsan'"); // returns the number of records for updating the database.
5. Main Methods of the ResultSet Interface
Void close ();
X getXXX (String columnName); or X getXXX (int columnIndex); // X indicates the data type; columnName indicates the column name; columnIndex indicates the column index, starting from 1