Detailed description of Oracle database instance operations through JDBC

Source: Internet
Author: User

Detailed description of Oracle database instance operations through JDBC

This example uses jdbc2.0 and Oracle9i databases. The database is located on the local machine. Use the Scott mode that comes with the database.

With detailed comments, I don't think I need to talk about it more.

Import java. SQL .*;
Import oracle. SQL .*;
Import oracle. JDBC. Pool. oracledatasource;

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

Conn. Commit ();
Rs = stmt.exe cutequery (
"Select ID, type_id, prod_name from product where id = 1 ";

// Assume that the type_id column is null.
System. Out. println ("ID =" + Rs. getint ("ID ");
System. Out. println ("type_id =" + Rs. getint ("type_id ");
If (Rs. wasnull ()){
System. Out. println ("type_id was null! ";
}
System. Out. println ("prod_name =" + Rs. getstring ("prod_name ");

// 9. method 2 for processing the null value of the database: Use the Java Wrapper class because the Java Wrapper class can be assigned a null value.
// Java. Lang. Integer typeid = (Java. Lang. integer) Rs. GetObject ("type_id ";
// System. Out. println (typeid); the value of typeid is null.
// When inserting or updating a row to the database with a null value, you can also use the Java package object
// Java. Lang. Double price = NULL;
// Int iiw.stmt.exe cuteupdate ("update products set price =" + Price + "where id = 12 ";

Rs. Close ();

// 10. Execute the data definition language statement (DDL: Create, alter, drop) ---- execute () to execute the DDL statement
// Executing DDL statements causes an implicit commit. Therefore, if you execute some uncommitted DML statements before issuing DDL statements, these DML statements will be submitted.

Boolean result = stmt.exe cute ("create table MERs (" +
"Id integer constraint customers_pk primary key," +
"First_name varchar2 (10) Not null," +
"Last_name varchar2 (10) Not null," +
"Dob date," +
"Phone varchar2 (15)" +
""
;
If (result = true ){
System. Out. println ("the table has created! ";
}
Else {
System. Out. println ("the table hasn't create ";
}
//-------------------------------------------------------------------------
}
Catch (exception e ){
System. Out. println ("error:" + E );
Try {
Conn. rollback ();
}
Catch (sqlexception sqle ){}
}
Finally {

Try {
If (RS! = NULL)
Rs. Close ();
}
Catch (sqlexception sqle ){
System. Out. println ("sqlstate:" + sqle. getsqlstate ());
System. Out. println ("sqlerrorcode: Error code" + sqle. geterrorcode ());
System. Out. println ("sqlerrormessage: Error string" + sqle. tostring ());
}

Try {
If (stmt! = NULL)
Stmt. Close ();
}
Catch (sqlexception sqle1 ){
System. Out. println ("sqlstate:" + sqle1.getsqlstate ());
System. Out. println ("sqlerrorcode: Error code" + sqle1.geterrorcode ());
System. Out. println ("sqlerrormessage: Error string" + sqle1.tostring ());
}

Try {
If (Conn! = NULL)
Conn. Close ();
}
Catch (sqlexception sqle2 ){
System. Out. println (sqle2.tostring ());
System. Out. println (sqle2.getsqlstate ());
System. Out. println (sqle2.geterrorcode ());
}

}

}
}

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.