Operation Oracle Database Instance Detailed (most common operation) via JDBC

zhaozj2021-02-16  35

This example uses JDBC2.0, and Oracle9i databases. The database is located on this unit. The Scott mode with the database is used.

I have a detailed note, I think I should talk more.

Import java.sql. *; import oracle.sql. *; import oracle.jdbc.pool.racledatasource;

Public class jdbcoacle {public static void main (string [] args) {

/ ** URL format: drivername: @driver_information 1, DRIVERNAME mainly has the following two JDBC: Oracle: Thin (Thin Driver) JDBC: Oracle: OCI (OCI Driver) 2, Driver_Information Host_Name: Port: Database_SID * /

Connection conn = null; statement rs = null; string url = "jdbc: oracle: Thin: @localhost: 1521: ORADB"; string username = "scott"; string password = "tiger"; try {

/ ** 1. Register the driver method. Class.Forname ("Oracle.jdbc.Oracledriver); * /

Drivermanager.RegisterDriver (New Oracle.jdbc.OracleDriver ());

// Second, open the database connection / ** method 1, use the Oracle data source object? Oracle.jdbc.pool.OracleDataSource DS = New Oracle.jdbc.pool.OracleDataSource (); DS.SetServerName ("localhost"); DS. SetDatabaseName ("ORADB"); // Data Stock Name DS.SetDrivertype ("OCI"); // The JDBC driver (OracleDataSore extension) DS.SETURL ("JDBC: Oracle: Thin: @localhost: 1521: ORADB "); // Specify the URL of the database (OracleDataSource Extension) DS.SetDataSourceName ("); // The name of the underlying data source DS.setNetworkProtocol ("TCP"); // DS.SetportNumber for database communication (1521); // port number DS.SETUSER ("Scott"); DS.SetPassword ("Tiger"); connection conn = ds.getConnection (); * / // method 2, use Drivermanger

Conn = drivermanager.getConnection (URL, UserName, Password);

// Set transaction submission mode //conn.setautocommit (true); // If automatic submission mode is prohibited, the automatic implicit submission is performed once when the Connection object is turned off 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 ();

// preparedState pStmt = conn.preparestatement ("SQL statement with parameters"); // callablestatement cstmt = conn.prepareCall ("Statement of the stored procedure"); // 4, get rows from the database / ** Select statement With executeQuery () insert, Update, DELETE statement with executeUpdate () If you don't know the SQL statement type to be executed, use Execute () * /

RS = Stmt.executeQuery ("SELECT ID, NAME, AGE, SEX, BIRTH FROM EMPLOYEE");

// 5, get 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 line to the database (Note: The monthly code starts from 0, so Month 1 represents February)

Java.sql.date date = new java.sql.date (82, 10, 05); int i = stmt.executeUpdate ("INSERT INTO Employee Values" "(1, 'QDS', 22, '1', to_date (DATE, 'YYYY, MM, DD') ")") ")"); // seven, modify the line in the data

INT j = stmt.executeUpdate ("Update Employee Set Age = 21 Where ID = 1"); // Eight, remove the line from the database

INT k = stmt.executeUpdate ("Delete from Employee Set = 1); // Nine, the NULL method of processing the database 1: Judgment of the Wasnull method for using the result set object

Conn.commit (); rs = stmt.executeQuery ("SELECT ID, TYPE_ID, PROD_NAME FROM Product Where ID = 1");

// This time it is assumed that type_id is listed as null value 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"); // nine NULL value method for processing the database: Using the Java wrapper. Because the Java wrapper can be assigned to null value //java.lang.integer typeid = (java.lang.integer) gtobject ("type_id"); //System.out.println (TypeID); At this time, the value of typeid is null // When you insert or update a behavior null value to the database, you can also use the Java wrapper object //java.lang.double price = NULL ; // int II = stmt.executeUpdate ("Update Products Set Price =" Price "Where ID = 12");

Rs.close ();

// 10, execute the data definition language statement (DDL: CREATE, ALTER, DROP) ---- Use the execute () method to execute the DDL statement // execute the DDL statement can cause an implicit submission, so if you send a DDL statement Some uncommitted DML statements have been performed before, then these DML statements will be submitted

boolean result = stmt.execute ( "create table customers (" "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 ("Table Table Hasn't create ");} // ---------------------------------------- ---------------------------------} catch (exception e) {system.out.println ("Error:" E); try {conn.rollback ();} catch (sqlexception sqle) {}} finally {

Try {if (rs! = null)} (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 ("SQLERRCODE: Error Code" SQLE1.GeterrorCode (); System.out.println "SQLERRORMESSAGE: Error condition" 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 ());

}

}

转载请注明原文地址:https://www.9cbs.com/read-21762.html

New Post(0)