Connect to and use a database using JDBC

Source: Internet
Author: User

JDBC Connection database Daquan:

1. JDBC connection to DB2
Class. forname ("com.ibm.db2.jdbc.net. db2driver ");
String url = "JDBC: DB2: // dburl: Port/dbname"
CN = drivermanager. getconnection (URL, susr, spwd );

2. JDBC connection to Microsoft sqlserver (Microsoft)
Class. forname ("com. Microsoft. JDBC. sqlserver. sqlserverdriver ");
CN = drivermanager. getconnection ("JDBC: Microsoft: sqlserver: // dbserverip: 1433; databasename = Master", susr, spwd );

Iii. JDBC connection to Sybase (jconn2.jar)
Class. forname ("com. Sybase. jdbc2.jdbc. sybdriver ");
CN = drivermanager. getconnection ("JDBC: Sybase: TDS: dbserverip: 2638", susr, spwd );

4. JDBC connection to MySQL (Mm. mysql-3.0.2-bin.jar)
Class. forname ("org. gjt. Mm. MySQL. Driver ");
CN = drivermanager. getconnection ("JDBC: mysql: // dbserverip: 3306/mydatabasename", susr, spwd );

V. JDBC connection to PostgreSQL (pgjdbc2.jar)
Class. forname ("org. PostgreSQL. Driver ");
CN = drivermanager. getconnection ("JDBC: PostgreSQL: // dbserverip/mydatabasename", susr, spwd );

6. JDBC connection to Oracle (classes12.jar)
Class. forname ("oracle. JDBC. Driver. oracledriver ");
CN = drivermanager. getconnection ("JDBC: oracle: thin: @ mydbcomputernameorip: 1521: orcl", susr, spwd );

VII. JDBC connection to ODBC
Class. forname ("Sun. JDBC. ODBC. jdbcodbcdriver ");
Connection Cn = drivermanager. getconnection ("JDBC: ODBC:" + sdsn, susr, spwd );

Note: exceptions must be captured and handled. The URL complies with the JDBC protocol.

These can also be found online.

Create a statement

• To execute SQL statements, you must obtain the java. SQL. Statement instance. The statement instance can be divided into the following types:

1. Execute static SQL statements. It is usually implemented through a statement instance.

2. Execute dynamic SQL statements. It is usually implemented through the preparedstatement instance.

3. Execute the database stored procedure. It is usually implemented through the callablestatement instance.

Specific implementation methods:

Statement stmt = con. createstatement ();

Preparedstatement pstmt = con. preparestatement (SQL );

Callablestatement cstmt = con. preparecall ("{call demosp (? ,?)} ");

 

Execute SQL statements

The statement interface provides three methods for executing SQL statements: executequery, executeupdate, and execute.

1. resultset executequery (string sqlstring): executes the SQL statement used to query the database and returns a result set object.

2,IntExecuteupdate (string sqlstring): used to execute insert, update, or delete statements and SQL DDL statements, such as CREATE TABLE and drop table.

3. Execute (sqlstring): it is used to execute statements that return multiple result sets, multiple update counts, or a combination of the two.

Specific implementation code:

Resultset rs = stmt.exe cutequery ("select * from ...");

IntRows = stmt.exe cuteupdate ("insert ...");

BooleanFlag = stmt.exe cute (string SQL );

 

Processing result

Two cases:

1. The number of records affected by this operation is returned when an update is executed.

2. The result returned by executing the query is a resultset object.

• The resultset contains all rows that meet the conditions in the SQL statement, and provides

The data in the row.

• Use the access method of the result set object to obtain data:

While(Rs. Next ()){

String name = Rs. getstring ("name ");

String pass = Rs. getstring (1); // This method is more efficient.

}

(Columns are numbered from left to right and start from column 1)

Disable JDBC objects

After the operation is complete, you need to close all the used JDBC objects to release the JDBC resources. The order of closure is the opposite of the declared order:

1. Disable record set

2. Close the statement

3. Close the connection object

If(RS! =Null) {// Close the record set

Try{

Rs. Close ();

}Catch(Sqlexception e ){

E. printstacktrace ();

}

}

If(Stmt! =Null) {// Close the Declaration

Try{

Stmt. Close ();

}Catch(Sqlexception e ){

E. printstacktrace ();

}

}

If(Conn! =Null) {// Close the connection object

Try{

Conn. Close ();

}Catch(Sqlexception e ){

E. printstacktrace ();

}

}

You can also close it together.

Try{

If(RS! =Null) {// Close the record set

Rs. Close ();

}

If(Stmt! =Null) {// Close the Declaration

Stmt. Close ();

}

If(Conn! =Null) {// Close the connection object

Conn. Close ();

}

 

}Catch(Sqlexception e ){

E. printstacktrace ();

}

 

 

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.