Operate Oracle databases through JDBC

Source: Internet
Author: User

Import c: \ oraclexe \ app \ oracle \ product \ 10.2.0 \ Server \ JDBC \ Lib \ ojdbc14.jar to the project attribute library in eclipse

PackageJDBC;

 

ImportJava. SQL. connection;

ImportJava. SQL. drivermanager;

ImportJava. SQL. resultset;

ImportJava. SQL. sqlexception;

ImportJava. SQL. statement;

 

Public class jdbcoracle {
Public static void main (string [] ARGs ){

/** URL format: drivername: @ driver_information
1. drivername mainly includes the following two types:
JDBC: oracle: thin (thin driver)
JDBC: oracle: OCI (OCI driver)
2, driver_information
Host_nameort: database_sid
*/

Connection conn = NULL;
Statement stmt = NULL;
Resultset rs = NULL;
String url = "JDBC: oracle: thin :@ localhost: 1521: oradb ";
String username = "Scott ";
String Password = "tiger ";
Try {

/** 1. register the driver
Method 1 class. forname ("oracle. JDBC. oracledriver ";
*/

Drivermanager. registerdriver (New Oracle. JDBC. oracledriver ());

// 2. Open the database connection
/** Method 1: use an ORACLE data source object?
Oracle. JDBC. Pool. oracledatasource DS = new Oracle. JDBC. Pool. oracledatasource ();
DS. setservername ("localhost ";
DS. setdatabasename ("oradb"; // data inventory name
DS. setdrivertype ("OCI"; // JDBC driver to be used (oracledatasore extension)
DS. seturl ("JDBC: oracle: thin: @ localhost: 1521: oradb"; // specify the URL of the database (oracledatasource extension)
DS. setdatasourcename (""; // name of the underlying data source
DS. setnetworkprotocol ("TCP"; // protocol used for Database Communication
DS. setportnumber (1521); // port number
DS. setuser ("Scott ";
DS. setpassword ("tiger ";
Connection conn = Ds. getconnection ();
*/
// Method 2: Use drivermanger

Conn = drivermanager. getconnection (URL, username, password );

// Set the transaction commit Mode
// Conn. setautocommit (true );
// If the automatic submission mode is disabled, an automatic implicit submission is executed when the connection object is closed to ensure that all DML statements that have not been submitted are automatically submitted.

Conn. setautocommit (false );

// 3. Create a JDBC Statement object

Stmt = conn. createstatement ();

// Preparedstatement pstmt = conn. preparestatement ("SQL statement with Parameters ";
// Callablestatement cstmt = conn. preparecall ("statements used to call a stored procedure ";
// 4. Obtain rows from the database
/** SELECT statement with executequery ()
Insert, update, and delete statements with executeupdate ()
If you do not know the type of SQL statement to be executed in advance, execute ()
*/

Rs = stmt.exe cutequery ("select ID, name, age, sex, birth from employee ";

// 5. Obtain rows from the database

While (Rs. Next ()){
Int id = Rs. getint ("ID ";
String name = Rs. getstring ("name ";
Int age = Rs. getint ("Age ";
String sex = Rs. getstring ("sex ";
Date birth = Rs. getdate ("birth ";
}
// Rs. Close ();
// 6. Add a row to the database (note: the encoding of the month starts from 0, so month 1 represents February)

Java. SQL. Date = new java. SQL. Date (82, 10, 05 );
Int I = stmt.exe cuteupdate ("insert into employee values" +
"(1, 'qds ', 22, '1', to_date (date, 'yyyy, mm, dd '))";
// 7. Modify the rows in the data

Int J = stmt.exe cuteupdate ("Update employee set age = 21 where id = 1 ";
// 8. delete a row from the database

Int K = stmt.exe cuteupdate ("delete from employee set id = 1 ";
// 9. method 1 for processing the null value of the database: Use the wasnull method of the result set object to determine

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/cyz1980/archive/2006/01/11/575929.aspx

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.