Basic operation of JDBC Access database for Java connection database

Source: Internet
Author: User

1. The JDBC driver for the database is loaded into classpath, and the JDBC driver of the target database product is usually copied to web-inf/lib in the actual development of the Java EE-based Web application.

2. load the JDBC driver and register it in DriverManager.

Oracle Database

Class.forName ("Oracle.jdbc.driver.oracleDriver");

SQL database

Class.forName ("Com.microsoft.sqlserver.jdbc.SQLServerDriver");

3. establish the database connection and obtain the Connection object .

SQL Database

String url= "Jdbc:sqlserve://localhost:1433;databasename=lizanqi";
String user= "SA";
String password= "SA";
Connection conn=drivermanager.getconnection (Url,user,password);

4. create a statement object or Perparedstatement object

Statement statement=conn.createstatement ();

PreparedStatement pstatement=conn.preparestatement (The SQL statement to execute);

Pstatement.setstring (the first placeholder, the contents of the placeholder);
Pstatement.setstring (the second placeholder, the contents of the placeholder);

Use PreparedStatement to avoid database injection attacks!

5. Execute SQL statements

String sql= "select * from table name";

ResultSet Rs=statement.executequery (SQL), or ResultSet rs=pstatement. ExecuteQuery ();

Execute the query statement with ExecuteQuery ();

However, you need to use executeupdate () when executing "Insert", "Update" "delete" statement.

6. Accessing the result recordset ResultSet object

General use while loop

while (Rs.next ()) {

Read the results sequentially.

}

7. Turn the ResultSet object, statement object, or Perparedstatement object Connection object off, and then release the resource that is occupied

Rs.close ();

Statement.close (); or pstatement.close ();

Conn.close ();

8. Summary

PreparedStatement:

1. Improved security to prevent injection attacks

2. Improved execution performance of database statements.

3. Improved readability and maintainability of the code.

Where the PreparedStatement interface inherits from the statement interface

What are the three things the JDBC API does:

1. Establish a connection to the database.

2. Execute the statement for the database.

3. Process the results.

DriverManager class: Manages the JDBC driver, depending on the database.

Connection interface: Responsible for connecting the database and serving the task of transmitting data.

Statement interface: Created by the Connection object, responsible for executing the database statement.

ResultSet interface: Responsible for saving the results of the query resulting from the execution of the statement object.

In addition to the ExecuteQuery method and the Executeupdate method, there is an Execute method that can execute all statements, and when True indicates that a resultset result set is returned.

Method:

Next (): Moves the cursor down one line from the current position.

Previous (): Cursor moves up one row from the current position.

void Close (): Closes the current object.

int getInt (int colindex) Gets the current line of the specified column number.

int getInt (String colindex) Gets the current row for the specified column name.

String getString (String collabel) gets the specified column name value for the current row of the result set.

Basic operation of JDBC Access database for Java connection database

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.