Use the methods in the java.sql package in the JDK for JDBC connections

Source: Internet
Author: User

The first step is to describe the JDBC connection using the java.sql package:

1. Load the driver for the database. (typically the data driver name for Oracle and mysql,oracle is: Oracle.jdbc.driver.OracleDriver mysql driver name is: com.mysql.jdbc.Driver)

2. Get the connection

3. Get precompiled objects,

4. Bind parameters.

5. Execute SQL

6. Get the result set and iterate through the results.

7. Close the connection.

The MySQL database is used here: the database name is as follows:

The code looks like this:

1  PackageCom.qls.mybatis.first;2 3 Importjava.sql.Connection;4 ImportJava.sql.DriverManager;5 Importjava.sql.PreparedStatement;6 ImportJava.sql.ResultSet;7 Importjava.sql.SQLException;8 9  Public classCrudjdbctest {Ten  One     /** A * Database connections are made using the pristine jdbc.  -      * @throwsSQLException -      */ the      Public Static voidMain (string[] args)throwsSQLException { -         //TODO auto-generated Method Stub -         //Connection interface, connection to a specific database.  -Connection connection=NULL; +         //Precompiled Objects -PreparedStatement preparedstatement=NULL; +         //result set AResultSet resultset=NULL; at         Try { -             //load driver: The MySQL driver is loaded here. The driver for MySQL is: Com.mysql.jdbc.Driver -Class.forName ("Com.mysql.jdbc.Driver"); -             /** - * Wms-elite Native beta is the database name of the connection - * Root: Is the user name.  in * a123456: is the password.  -              */ toconnection=Drivermanager.getconnection ( +"Jdbc:mysql://localhost:3306/wms-elite native beta? useunicode=true&characterencoding=utf8&allowmultiqueries= True,  -"Root", "a123456"); the             //get PreparedStatement object: *String sql= "Select *from user where address=?"; $Preparedstatement=connection.preparestatement (SQL);Panax Notoginseng             //Binding Parameters: -Preparedstatement.setstring (1, "Beijing"); the             //execute the query and query the results.  +resultset=preparedstatement.executequery (); A              while(Resultset.next ()) { theSystem.out.println ("username is:" +resultset.getstring ("username") + "\ n" + "Gender is:" +resultset.getstring (4)); +             } -}Catch(Exception e) { $ e.printstacktrace (); $}finally{ -             /** - * Close the database connection.  the * The Order of closing is: Resultset,preparedstatement,connection. -              */Wuyi             if(resultset!=NULL) { the                  -Resultset.close ();//Close Result set Wu             } -             if(preparedstatement!=NULL) { About                  $Preparedstatement.close ();//Close Precompiled Objects -             } -             if(connection!=NULL) { -                  AConnection.close ();//Close the connection.  +             } the              -         } $     } the  the}/*Output: the user name is: Zhang San the Gender is: 1 - *///:~

The above code basically commented on the important statements. Output Result:

User name is: 369 sex is: 1
See the following:

Use the methods in the java.sql package in the JDK for JDBC connections

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.