JDBC programming steps:
1. Load the driver to load the driver (register the driver)
(1) class. forname ("com. MySQL. JDBC. Driver"); | class. forname (). newinstance () | new drivername ();
// New drivername () is connected by new COM. MySQL. JDBC. Driver ();
(2) It automatically registers with drivermanager during instantiation and does not need to explicitly call the drivermanager. registerdriver method.
2. Connect to database
(1) drivermanager. getconnection ()
3. Execute the SQL
(1) connection. createstatement ()
(2) statement.exe cutequery ()
(3) statement.exe cuteupdate ()
4. retireve the result data
(1) loop to get the result while (Rs. Next ())
5. Show the result data
(1) convert various data types in data to the getxxx method in Java
6. Close
1. Close the resultset/Close the statement/Close the connection
Demo
- /**
- * The code is an example of getting started. It is mainly used to familiarize yourself with the basic process of JDBC programming. Data is not displayed on the page, and exceptions are not considered in detail.
- */
- Import java. SQL .*;
- Public class testjdbc {
- /**
- * @ Param ARGs
- */
- Public static void main (string [] ARGs) throws exception
- {
- // 1. Load the driver to load the driver (register the driver)
- New COM. MySQL. JDBC. Driver ();
- // Or use this connection method // class. forname ("com. MySQL. JDBC. Driver ");
- // 2. Connect to database to connect to the database. Take mysql5.0 as an example. The database name is college, the user name is root, and the password is 123456.
- Connection conn = drivermanager. getconnection ("JDBC: mysql: // localhost: 3306/College", "root", "123456 ");
- // 3. Execute the SQL
- Statement stmt = conn. createstatement ();
- Resultset rs = stmt.exe cutequery ("select * from user ");
- While (Rs. Next () // 4. retireve the result data traverses the result set
- {
- // 5. Show the result data to display the result. convert various data types in the data to the getxxx method in Java.
- System. Out. println ("the first Colum :");
- System. Out. println (Rs. getstring ("username "));
- System. Out. println ("the second Colum :");
- System. Out. println (Rs. getstring ("userflag "));
- }
- // 6. Close the connection and close it in reverse order from the start.
- Rs. Close ();
- Stmt. Close ();
- Conn. Close ();
- }
- }
Make the demo a little better, as shown below:
- Import java. SQL .*;
- Public class testjdbc
- {
- /**
- * @ Param ARGs
- */
- Public static void main (string [] ARGs)
- {
- Connection conn = NULL;
- Statement stmt = NULL;
- Resultset rs = NULL;
- Try
- {
- // 1. Load the driver to load the driver (register the driver)
- Class. forname ("com. MySQL. JDBC. Driver ");
- // Or use this connection method // new COM. MySQL. JDBC. Driver ();
- // 2. Connect to database
- // Connect to the database. Take mysql5.0 as an example. The database name is college, the username is root, and the password is 123456.
- Conn = drivermanager. getconnection (
- "JDBC: mysql: // localhost: 3306/College", "root", "123456 ");
- // 3. Execute the SQL
- Stmt = conn. createstatement ();
- Rs = stmt.exe cutequery ("select * from user ");
- While (Rs. Next () // 4. retireve the result data traverses the result set
- {
- // 5. Show the result data to display the result. convert various data types in the data to the getxxx method in Java.
- System. Out. println ("the first Colum :");
- System. Out. println (Rs. getstring ("username "));
- System. Out. println ("the second Colum :");
- System. Out. println (Rs. getstring ("userflag "));
- }
- } Catch (classnotfoundexception E)
- {
- E. printstacktrace ();
- } Catch (sqlexception E)
- {
- E. printstacktrace ();
- } Finally
- {
- // 6. Close the connection and close it in reverse order from the start.
- Try
- {
- If (RS! = NULL)
- {
- Rs. Close ();
- Rs = NULL;
- }
- If (stmt! = NULL)
- {
- Stmt. Close ();
- Stmt = NULL;
- }
- If (Conn! = NULL)
- {
- Conn. Close ();
- Conn = NULL;
- }
- } Catch (exception E)
- {
- E. printstacktrace ();
- }
- }
- }
- }