JDBC-java database connection steps, jdbc-java Database

Source: Internet
Author: User

JDBC-java database connection steps, jdbc-java Database

Java database connection procedure

1. Load the driver

Class. forName ("com. mysql. java. Driver ");

2. Create a connection object

DriverManager class method:

Connection DriverManager. getConnection (String url); or Connection DriverManager. getConnection (String url, String user, String password );

Instance:

Connection conn = DriverManager. getConnection ("jdbc: mysql: // localhost: 3306/student? User = root & password = & characterEncoding = UTF-8 ");

Or

Connection conn = DriverManager. getConnection ("jdbc: mysql: // localhost: 3306/student", "root ","");

3. Create an SQL Execution object

Connection interface method:

Statement createStatement (); or PreparedStatement prepareStatement (String SQL );

Instance:

Statement stmt = conn. createStatement ();

Or

PreparedStatement ps = connn. prepareStatement ("select * from studentInfo ");

4. Create a ResultSet result set object

Statement interface method:

ResultSet executeQuery (String SQL );

Int executeUpdate (String SQL );

ResultSet getResult ();

PreparedStatement interface method:

Void setXXX (int paramIndes, XXX value); // XXX indicates the data type, such as int or String

Instance:

ResultSet rs = stmt.exe cuteQuery ("select * from studentInfo"); // return the query result set

Int num = stmt.exe cuteUpdate ("delete from studentInfo where name = 'hangsan'"); // returns the number of records for updating the database.

5. Main Methods of the ResultSet Interface

Void close ();

X getXXX (String columnName); or X getXXX (int columnIndex); // X indicates the data type; columnName indicates the column name; columnIndex indicates the column index, starting from 1

  

 

 

 

 

 

 

 


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.