Connect to the Oracle database using Java --- the thin method of JDBC

Source: Internet
Author: User

When using this method, you need to add the Oracle jar package to the classpath variable. This package can be found in $ ORACLE_HOME/jdbc/lib/classes12.jar of the oralce client program.

Import java. SQL .*;

Public class jdbcthin {
// Dburl database connection string information, where "1521" is the port and "ora9" is the SID
String dburl = "JDBC: oracle: thin: @ 10.10.20.15: 1521: ora9 ";
// Theuser is the Database User Name
String theuser = "sman ";
// Thepw indicates the Database Password.
String thepw = "sman ";
// Several database variables
Connection c = NULL;
Statement conn;
Resultset rs = NULL;

// Initialize the connection
Public jdbcthin (){
Try {
Class. forname ("oracle. JDBC. Driver. oracledriver"). newinstance ();
// Establish a connection with the data source specified by the URL
C = drivermanager. getconnection (dburl, theuser, thepw );
// Use statement for query
Conn = C. createstatement ();
} Catch (exception e ){
E. printstacktrace ();
}
}

// Execute the query
Public resultset executequery (string SQL ){
Rs = NULL;
Try {
Rs = conn.exe cutequery (SQL );
} Catch (sqlexception e ){
E. printstacktrace ();
}
Return Rs;
}

Public void close (){
Try {
Conn. Close ();
C. Close ();
} Catch (exception e ){
E. printstacktrace ();
}
}

Public static void main (string [] ARGs ){
Resultset newrs;
Jdbcthin newjdbc = new jdbcthin ();
Newrs = newjdbc.exe cutequery ("select * From eventtype ");
Try {
While (newrs. Next ()){
System. Out. Print (newrs. getstring ("event_type "));
System. Out. println (":" + newrs. getstring ("content "));
}
} Catch (exception e ){
E. printstacktrace ();
}

Newjdbc. Close ();
}
}

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.