MySQL database usage flow, code interpretation

Source: Internet
Author: User

Process of using the database:
1. Registration drive;
2. Use the Drivermanager.getconnection method to obtain the Connection object con;
A method:
3. Use the Createstatement () method of the Connection object to get the object stmt that can execute the SQL statement;
4.stmt Execute Query (query statement);
b Method (recommended):
3. Using the Preparestatement method of the Connection object, the preprocessing object pstmt with parameter (query statement) is obtained.
4.PSTMT executes the query and assigns the result set to the ResultSet object;

1  Public classdbtest{2      Public Static voidMain (string[] args) {3         /**4 * Use of the database:5 * 1. Registration drive;6 * 2. Use the Drivermanager.getconnection method to obtain the Connection object con;7 * A method:8 * 3. Use the Createstatement () method of the Connection object to get the object stmt that can execute the SQL statement;9 * 4.stmt Execution Query (query statement);Ten * B Method (recommended): One * 3. Using the Preparestatement method of the Connection object, the preprocessing object pstmt with parameter (query statement) is obtained. A * 4.PSTMT executes the query and assigns the result set to the ResultSet object; -          */ -         Try { theClass.forName ("Com.mysql.jdbc.Driver");//Registration Driver -Connection con = drivermanager.getconnection ("Jdbc:mysql://localhost:3306/book", "root", "root123");//Get the Connection object -                 //using the statement method to handle the execution of SQL statements is slightly different from the PreparedStatement method.  -Statement stmt=con.createstatement ();//get an object that can execute SQL statements +ResultSet rs=stmt.executequery ("select * from BookInfo where id=1");//executes the query and assigns the result set to the ResultSet object -                 /**The following methods are recommended: + preparedstatement pstmt=con.preparestatement ("select * from BookInfo where id=1");//Get Preprocessed object A ResultSet rs=pstmt.executequery (); Executes the query and assigns the result set to the ResultSet object at                  */ -                  while(Rs.next ()) -{intId=rs.getint (1); -System.out.println (rs.getstring (2) + "" "+rs.getstring (3) +" "+rs.getstring (4)); -                 } -}Catch(ClassNotFoundException e) {//The capture driver class could not find the exception in E.printstacktrace ();  -}Catch(SQLException e) {//Capturing SQL Exceptions to e.printstacktrace (); +             } -         } the}

MySQL database usage flow, code interpretation

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.