Java connects MySQL database with JDBC two (2)

Source: Internet
Author: User

This article is an improvement on Java using JDBC to connect MySQL database II.

The previous section uses PreparedStatement to execute the database statement, but PreparedStatement needs to pass an SQL statement parameter before it can be created. However, the DBHelper class is just a function of opening and closing the database, so the SQL statement is placed in the application-level section rather than in the DBHelper class.

Statment does not need to pass an SQL statement parameter to create it.

The modification section is as follows:

 Public classdbhelper {String driver= "Com.mysql.jdbc.Driver"; String URL= "Jdbc:mysql://localhost:3306/test"; String User= "Root"; String Password= "123456";  PublicConnection Conn; //Public PreparedStatement PST;     PublicStatement Statement;  PublicDBHelper () {Try {            //Load DriverClass.forName (driver); //Continuous Databaseconn =(Connection) drivermanager.getconnection (URL, user, password); if(!conn.isclosed ()) {System.out.println ("Succeeded connecting to the database!"); }               //PST = (preparedstatement) conn.preparestatement (SQL);//using preparestatement to execute SQL statementsstatement = (statement) conn.createstatement ();//using statement to execute SQL statements}Catch(Exception e) {e.printstacktrace (); }    }         Public voidClose () {Try {             This. Conn.close (); //this.pst.close ();             This. Statement.close (); } Catch(Exception e) {e.printstacktrace (); }    }}

 Public classJdbctest { Public Static voidMain (string[] args) {String SQL= "SELECT * FROM Employee";//SQL statements        Try{            //DBHelper db = new dbhelper (SQL);//Create a DBHelper object//ResultSet rs = (ResultSet) db.pst.executeQuery ();//return result setDBHelper db =NewDBHelper ();//Create a DBHelper objectResultSet rs = (ResultSet) db.statement.executeQuery (SQL);//return result setSystem.out.println ("-----------------"); System.out.println ("Name" + "\ T" + "Mailbox" + "\ T" + "date"); System.out.println ("-----------------");  while(Rs.next ()) {//get the data in the result setString uname = rs.getstring ("name"); String Uemail= rs.getstring ("email"); String uhiredate= Rs.getstring ("HireDate"); //Output ResultsSystem.out.println (uname + "\ T" + Uemail + "\ T" +uhiredate);            } rs.close (); Db.close ();//Close Connection}Catch(SQLException e) {e.printstacktrace (); }    }}

Java connects MySQL database with JDBC two (2)

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.