1 PackageIt.cast.jdbc;2 3 Importjava.sql.Connection;4 ImportJava.sql.DriverManager;5 ImportJava.sql.ResultSet;6 Importjava.sql.SQLException;7 Importjava.sql.Statement;8 9 Public classBase {Ten One Public Static voidMain (string[] args)throwsSQLException { A test (); - } - the Static voidTest ()throwsSQLException { - //1. Registration Driver -Drivermanager.registerdriver (Newcom.mysql.jdbc.Driver ()); - + //2. Establish a connection -Connection conn =Drivermanager.getconnection ( +"Jdbc:mysql://localhost:3306/jdbc", "root", "123"); A at //3. Create a statement -Statement st =conn.createstatement (); - - //4. Execute the statement -ResultSet rs = st.executequery ("SELECT * from User"); - in //5. Processing Results - while(Rs.next ()) { toSystem.out.println (Rs.getobject (1) + "\ T" + rs.getobject (2) + "\ T" ++ Rs.getobject (3) + "\ T" + rs.getobject (4)); - } the * //6. Releasing Resources $ rs.close ();Panax Notoginseng st.close (); - conn.close (); the } + A}BASE
Import Jar ==>mysql-connector-java-5.1.22-bin.jar