JDBC Connection Database Process

Source: Internet
Author: User
Tags stmt

JDBC Connection Database process: (Exception code ignored, for example, MySQL, other databases, drivers and URLs to refer to other information on the Internet bar)

1. Load the JDBC Driver

Class.forName ("Com.mysql.jdbc.Driver");

2. Create a connection to the database

String url= "JDBC:MYSQL://LOCALHOST:3306/TEST?USEUNICODE=TRUE&CHARACTERENCODING=GBK";

String username= "Your user name";

String pwd= "Your password";

Connection conn=drivermanager.getconnection (URL,USERNAME,PWD);

3.

    Create a statement         to execute the SQL statement, Java.sql.Statement instances must be obtained, statement instances are divided into the following 3        types:         1), execute static SQL statements. Typically implemented through statement instances.          2), execute dynamic SQL statements. Typically implemented through PreparedStatement instances.          3), execute database stored procedures. Typically implemented through CallableStatement instances.        Specific implementation:           Statement stmt = Con.createstatement ();          PreparedStatement pstmt = Con.preparestatement (SQL);          callablestatement cstmt =  Con.preparecall ("{Call Demosp (?,?)}");   4. The      Execute SQL statement    Statement Interface provides three ways to execute SQL statements: ExecuteQuery, executeupdate, and execute         1), ResultSet executeQuery (String sqlString): Executes the SQL statement that queries the database  &NBSp         Returns a result set (ResultSet) object.         2),intExecuteupdate (String sqlString): Used to execute INSERT, UPDATE, or DELETE statements, as well as SQL DDL statements such as CREATE table and drop table 3), exe      Cute (sqlString): Used to perform statements that return multiple result sets, multiple update counts, or a combination of both. Specifically implemented code: ResultSet rs = stmt.executequery ("SELECT * from ...");introws = Stmt.executeupdate ("INSERT into ...");BooleanFlag = Stmt.execute (String sql);        5. Processing results in two cases: 1), the execution of the update returns the number of records affected by this operation.       2), the result of executing the query returned is a ResultSet object.       resultset contains all rows that conform to the conditions in the SQL statement, and it provides access to the data in those rows through a set of Get methods. • Get data using the access method of the result set (ResultSet) object: while(Rs.next ())            {String name = rs.getstring ("name"); String pass = rs.getstring (1);    This method is more efficient} (the column is numbered from left to right and starts at column 1) 6. Close the resource operation to close all the JDBC objects used to release the JDBC resource, close the order and declaration order opposite: 1), close the Recordset 2), close the Declaration 3), close the Connection object

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.