Java Simple console program for MySQL data read

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.