New Console Application Application client project
Introducing JAR Packages
Import Java.sql.connection;import Java.sql.drivermanager;import Java.sql.preparedstatement;import Java.sql.resultset;import java.sql.SQLException;
Introducing MySQL connection on the jar package
Item Properties->build path–> Add extend jar
mysql-connector-java-5.1. -bin.jar
Connection Connection =NULL; //Precompiled statement, using precompiled statement to improve database performancePreparedStatement PreparedStatement =NULL; //result setResultSet ResultSet =NULL; Try { //Load Database driverClass.forName ("Com.mysql.jdbc.Driver"); //get a database link by driving the management classConnection =DriverManager. getconnection ("Jdbc:mysql://localhost:3306/mybatis?characterencoding=utf-8", "Root","123456"); //Define SQL statements? represents a placeholderString sql ="SELECT * from users where name =?"; //Get preprocessing statementPreparedStatement =connection.preparestatement (SQL); //set the parameter, the first parameter is the ordinal of the parameter in the SQL statement (starting at 1), the second parameter is the parameter value setPreparedstatement.setstring (1,"user aloof and pale wolf"); //issuing SQL execution queries to the database, querying the result setResultSet =Preparedstatement.executequery (); //traversing query result sets while(Resultset.next ()) {System. out. println (Resultset.getstring ("ID") +" "+ resultset.getstring ("name")); } } Catch(Exception e) {e.printstacktrace (); } finally { //Freeing Resources if(ResultSet! =NULL) { Try{resultset.close (); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); } } if(PreparedStatement! =NULL) { Try{preparedstatement.close (); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); } } if(Connection! =NULL) { Try{connection.close (); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); } } }
Java Simple console program for MySQL data read