Java connection to Oracle class, provides operation interface

Source: Internet
Author: User

To use this class, use the jar package to connect to Oracle.

 

Steps:

1. Load the driver

2. Establish a connection

3. Obtain the SQL statement object (statement)

4. Execute SQL statements

5. release resources

 

 

 

Import Java. SQL. connection; import Java. SQL. drivermanager; import Java. SQL. resultset; import Java. SQL. sqlexception; import Java. SQL. statement;/** @ author sunnycoffee * @ time 2011-9-16 * todo tool class used to connect to the database */public class dbconnection {private connection conn; private statement Stat; private string URL; private string user; private string password; // obtains the statement object public dbconnection () {try {class. forname ("oracle. JDBC. driver. oracledriver "); // load the driver url =" JDBC: oracle: thin: @ localhost: 1521: orcl "; user =" system "; // database Username Password = "myoracle"; // Database Password conn = drivermanager. getconnection (URL, user, password); // get the connection stat = Conn. createstatement (); // get statement} catch (classnotfoundexception e) {// todo auto-generated catch blocke. printstacktrace ();} catch (sqlexception e) {// todo auto-generated catch blocke. printstacktrace () ;}}// insert, modify, and delete interfaces: Public int executeupdate (string SQL) {int flag = 0; try {flag = stat.exe cuteupdate (SQL ); // Execute SQL statement} catch (sqlexception e) {// todo auto-generated catch blocke. printstacktrace ();} finally {try {stat. close (); // release the resource Conn. close ();} catch (sqlexception e) {// todo auto-generated catch blocke. printstacktrace () ;}} return flag;} // The Public resultset executequery (string SQL) {resultset set = NULL; try {set = stat.exe cutequery (SQL );} catch (sqlexception e) {// todo auto-generated catch blocke. printstacktrace ();} finally {// try {// STAT. close (); // Conn. close (); //} catch (sqlexception e) {// todo auto-generated Catch Block // E. printstacktrace (); //} return set ;}}

Url = "JDBC: oracle: thin: @ localhost: 1521: orcl"; meaning:

JDBC Java Database Connection

Oracle Database type to be connected

Thin client in thin connection mode

@ Localhost: 1521 IP address and port number of the database

Orcl Database Name

 

The query interface returns a set. If the connection is closed, the set. Next method is unavailable.

Related Article

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.