Haven't written about database applications for a long time, here's a review of Java JDBC.
1. Using Java JDBC to operate a database typically requires 6 steps:
(1) Build the JDBC Bridge and load the database driver;
(2) Connect to the database, get Connection object (use database connection address, user name, password);
(3) Acquiring database statement objects;
(4) Perform database operations;
(5) Read the result;
(6) Close the database connection;
2. Using Java JDBC Operational database (MySQL) code:
To connect to the MySQL database, you need to import the MySQL database jar package, this code uses Mysql-connector-java-5.1.18-bin.jar.
1 Importjava.sql.Connection;2 ImportJava.sql.DriverManager;3 Importjava.sql.Statement;4 ImportJava.sql.ResultSet;5 6 Public classMyTest {7 Public Static voidMain (String args[]) {8Connection con =NULL;9Statement st =NULL;TenResultSet rs =NULL; One Try { A //get an instance of MySQL driver -Class.forName ("Com.mysql.jdbc.Driver"). newinstance (); - //Get the Connection object (provided: address, user name, password) thecon = drivermanager.getconnection ("Jdbc:mysql://127.0.0.1/weather", "root", "root"); - - if(!con.isclosed ()) -System.out.println ("Successfully connected"); + Else -System.out.println ("Failed Connected"); + A //Create a statement, database object atSt =con.createstatement (); - //run the SQL query statement -rs = St.executequery ("select * from Weather.question_type_1;"); - //Reading result sets - while(Rs.next ()) { -System.out.println ("Column1:" +rs.getint (1)); inSystem.out.println ("Column2:" +rs.getstring (2)); -System.out.println ("Column3:" +rs.getstring (3)); toSystem.out.println ("Column4:" +rs.getstring (4)); + } - //Close Link the con.close (); *}Catch(Exception e) { $System.err.println ("Exception:" +e.getmessage ());Panax Notoginseng } - } the}