ORACLE_JDBC operation instructions and code examples

Source: Internet
Author: User
Tags savepoint

ORACLE_JDBC operation instructions and code examples
JDBC: // JDBC basic operation steps: 1 load the driver 2 get the link object 3 link 4 transfer data, database processing, accept data result set 5 process result set 6 close the resource // Add: 1. Load the driver in three ways // The first Class. forName ("oracle. jdbc. driver. oracleDriver "). newInstance (); // The second Class. forName ("oracle. jdbc. driver. oracleDriver "); // The third type is new oracle. jdbc. driver. oracleDriver (); 2 link database // 2 link database Connection conn = DriverManager. getConnection ("full path", "User Name", "password"); Connection conn = DriverManager. getConnection ("j Dbc: oracle: thin: @ 127.0.0.1: 1521: orcl "," system "," root "); 3. Create a Statement object // 3. Create a Statement object Statement stmt = conn. createStatement (); ResultSet rs = stmt.exe cuteQuery ("select * from c # scott. emp "); // the table to be queried 4. Obtain the result next () cyclically; // the return value is boolean true/false, indicating that the next bit has no elements, if yes, true is returned. Otherwise, false while (rs. next () {// judge whether there is any value next. 5. in the loop, convert the data type to get the desired data type (the data type corresponding to java). You can convert the data type at will, as long as the precision is not lost, System. out. print (rs. getString ("empno") + ""); // r S. getIn ("empno"); also, because emono uses the number type System. out. print (rs. getString ("ename") + ""); System. out. println (rs. getString ("job") + "");} 6 close resource close (); // close resource rs. close (); stmt. close (); conn. close (); executeQuery: executeQuery (); // the return value of the query statement is an array after receiving the result in the ResultSet type. Each element in the array is the object of a row in the table. Connection conn = DriverManager. getConnection ("jdbc: oracle: thin: @ 127.0.0.1: 1521: orcl", "system", "root"); Statement stmt = co Nn. createStatement (); // create the statement object ResultSet rs = stmt.exe cuteQuery ("select * from c # scott. emp "); // The type of the return value of the statement to be executed for the table to be queried is ResultSet type next: next (); // The return value is boolean true/false. If the next bit has no elements, true is returned. Otherwise, false Connection conn = DriverManager is returned. getConnection ("jdbc: oracle: thin: @ 127.0.0.1: 1521: orcl", "system", "root"); Statement stmt = conn. createStatement (); // create the statement object ResultSet rs = stmt.exe cuteQuery ("select * fr Om c # scott. emp "); // the return value type of the statement to be executed for the table to be queried is the result of the ResultSet type, which is equivalent to an array. Each array element is a row object rs. next (); // obtain the next element (ROW) getString: statement.exe cuteUpdate (SQL); getString (); // obtain the string executeUpdate: executeUpdate (""); // execute the string command to add insert... for example, String SQL = "insert into c # scott. a_tab values (123, 'xiaomei', 'beijing ~ ') "; Statement.exe cuteUpdate (SQL); // The add command will be modified in the result set only when the result set getInt: getInt (); // get the integer value executeUpdate: executeUpdate (); insert the statement into the specified table String SQL = "insert into c # scott. a_tab values (123, 'xiaomei', 'beijing ~ ') "; Statement.exe cuteUpdate (SQL); DriverManager_getConnection: DriverManager. getConnection (); // The Connection database is of the Connection type, for example, // Connection conn = DriverManager. getConnection (full path, user name, password); Connection conn = DriverManager. getConnection ("jdbc: oracle: thin: @ 127.0.0.1: 1521: orcl", "system", "root"); createStatement: // createStatement () without parameters (); // create a Statement object and use the Connection type call to return the Statement type receiving result, for example, Statement stmt = conn. cr EateStatement (); // createStatement (int resultSetType, int resultSetConcurrency) with parameters; // parameter 1 resultSetType-result set type: It is ResultSet. TYPE_FORWARD_ONLY, ResultSet. TYPE_SCROLL_INSENSITIVE // if this is written, you can use all the methods in the ResultSet or ResultSet. one of TYPE_SCROLL_SENSITIVE // parameter 2 resultSetConcurrency-concurrency type; it is ResultSet. CONCUR_READ_ONLY // query or ResultSet. one of CONCUR_UPDATABLE // Add/change/delete (Add/delete/modify) createStatement (parameter 1, parameter 2 );// If parameter 1 is not specified, only the next () method of ResultSet can be called. If parameter 1 is specified, PreparedStatement: PreparedStatement s = conn can be called. preparedStatement ("insert into c # scott. a_tab values (?,?,?) "); // Pre-prepared string. Use the value of values? Instead, we don't know what type it is. To change the result set, we need to execute the statement: execute (); execute: s.exe cute (); // The execution method of each created statement object is different. setInt: setInt (); // set the question mark (s. setInt (); // The first 1 represents the first question mark and the last 11 represents the value to be added setString: setString (); // you can specify the above question mark as the string s. setString (2, "sad"); // The first two represent the second question mark, followed by "sad", representing the value parseInt: parseInt (); // It is an Integer method (int encapsulation type), which converts a string of only numbers to int type int I = Integer. parseInt ("123"); // converts it to 123 addBatch: addBatch (); // Add multiple statements, for example, s. setInt (1,123); s. setString (2, "234"); s. setString (3, "456"); s. addBatch (); // This is a piece of s. setInt (1,123); s. setString (2, "234"); s. setString (3, "456"); s. addBatch (); // here are two items. // The above is just a string and command, but it is not executed. // execute multiple statements of addBatch () and use executeBatch () executeBatch: s.exe cuteBatch (); // Add multiple statements to execute commit: commit (); // force submit Connection conn = DriverManager. getConnection ("jdbc: oracle: thin: @ 127.0.0.1: 1521: orcl", "system", "root"); // connect to the database conn. commit (); // it is mandatory to submit setAutoCommit: setAutoCommit (); // set to automatically submit (if no command is entered, submit it once), true/false conn. setAutoCommit (false); // set not to automatically submit conn. setAutoCommit (true); // set savepoint for automatic submission: // Add a set rollback point savepoint a; // set the name of the rollback point to a rollback_to: // rollback operations before the rollback point. All operations after the rollback point are equal to no rollback to a (rollback name); // rollback to a point rollback: rollback (); // rollback. If no rollback point is set, it will roll back to the last submitted place and cancel all the rollback points. rollback (); // roll back the ResultSet_Li_Mian_De_Fang_Fa: // query: only the result set ResultSet rs = stmt.exe cuteQuery ("select * from c # scott. a_tab "); next: next (); // the return value is boolean true/false. If there is no element in the next position, true is returned. Otherwise, false rs is returned. next (); // The cursor points to the next last: last (); // points to the last (last column) pointer in the last column rs. last (); // point the cursor to the last position (the last column) of the rs result set isLast: isLast (); // determine whether the cursor points to the last position. The return value is boolean, true/false rs. isLast (); // determines whether the current cursor points to the last absolute: absolute () of the rs result set; // points the cursor to the specified location rs. absolute (3); // point the cursor to the third element of the rs result set, previous: previous (); // point the cursor to the previous position of the current cursor // the absolute (3) above; didn't the cursor point to the third position rs. previous (); // now it is the second place, that is, move the current cursor up one bit // Add: moveToInsertRow (); // a buffer, it can be considered that it is located below the last row of the result set (or it can be said that a new blank row is created at the bottom of the result set). When there is a buffer, no matter where the cursor points, the default operation is this buffer, until the buffer is added to the result set and database rs. moveToInsertRow (); // in the last row of the rs result set, create an empty row updateInt: updateInt (); // Add the column (number type) value rs. updateInt (9999); // Add the value or: rs to the first column (the first field. updateInt ("ID", 9999); // or add the value updateString: updateString (); // Add (or change) the value rs of the column (varchar2 type) through the specified column. updateString (2, "youname"); // the second column (the second field), with the value "youname" Or: rs. updateString ("ENAME", "youname"); // Add the value insertRow: insertRow () through the specified column; // Add the newly added line and the content in it, added to the result set and to the database rs. insertRow (); // Add the new row set above to the result set and database moveToCurrentRow: moveToCurrentRow (); // move the cursor to the new row rs. moveToCurrentRow (); // in the rs result set, move the cursor to the new row deleteRow: deleteRow (); // Delete the row pointed to by the current cursor, and delete rs in the result set and database. deleteRow (); // Delete the row pointed to by the cursor in the rs result set and Database: updateString (); //// modify the data rs in the in-memory dataset. updateString ("hello", "hellonimei"); // modify the data in the in-memory dataset (column: hello, value: hellonimei) updateRow: updateRow (); // update the changed data to the database rs. updateRow (); // update the changes to the database, which is a database. close: close (); // close the resource from the bottom up. For example, when opening the resource, first enable conn, then stmt, and then shut down rs, then close stmt, and close the conn/1 driver object new oracle. jdbc. driver. oracleDriver (); // 2 Connection object Connection conn = DriverManager. getConnection ("jdbc: oracle: thin: @ 127.0.0.1: 1521: orcl", "c # scott", "root"); // 3 transmission object Statement stmt = conn. createStatement (ResultSet. TYPE_SCROLL_INSENSITIVE, ResultSet. CONCUR_UPDATABLE); // concurrent updatable, compared with the previous one // 4 result set object ResultSet rs = stmt.exe cuteQuery ("select * from emp"); rs. close (); stmt. close (); conn. close ();

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.