I have n’t written about database applications for a long time. Here I review Java JDBC.
1. Using Java JDBC to operate database generally requires 6 steps:
(1) Create the JDBC bridge and load the database drive;
(2) Connect the database and get the Connection object (using the database connection address, user name, password);
(3) Obtain the database statement object;
(4) Perform the database operation;
(5) Read the results;
(6) Close the database connection;
2. Use Java JDBC to operate database (MySQL) code:
Connecting the MySQL database, you need to import the MySQL database jar package. This code uses mysql-confor-java-5.1.18-bin.jar.
Import java.sql.connection; Import Java.SQL.DriverManager; Import Java.Sql.Statement; Import Java.sql.ResultSet; c void main (string args []) {connection con = null; statement st = Null; ResultSet RS = NULL; Try {// Obtain mysql -driven instance class.Forname ("com.mysql.jdbc.driver"). Newinstance (); ) Con = DriverManager.getConnection ("JDBC: MySQL: //127.0.0.1/weather", "root", "root"); if (! Con.isclosed ()) System.out.println ("Successfully C onnected ") ; else system.out.println ("failed connected"); // Create a statement, the database object str to con.createStatement (); // Run the SQL query statement RS = st.executequery ("Select * FROM Weat Her.question_type_1; "); // Read the result set while (rs.next ()) {system.out.println (" column1: "+rs.getIntint (1)); System.out.println (" column2: "+rs. getString (2)); System.out.println ("Column3:"+RS.GetString (3)); System.out.println ("Column4:"+RS.getString (4)); .close ();} Catch (Exception E) {System.err.println ("Exception:" + E.getMessage ());