Java Database Basic Operation 1, Java Database Operation Basic Process 2, Several Common Important Tips: Scroll, updated record set bulk update transaction processing
Java database operation Basic process: acquired database connection - execute SQL statement - Processing Execution Results - Release Database Connection 1 Driver.racledriver "; URL =" JDBC: Oracle: Thin: @ 127.0.0.1: 1521: ORASVR; UID = "System"; PWD = "manager"; class.forname (classname); connection cn = drivermanager.getConnection (URL , UID, PWD); 2) String JNDI = "JDBC / DB" in JNDI (Java Naming and Directory Service); Context CTX = (Context) New InitialContext (). Lookup ("Java: Comp / ENV") DataSource DS = (Datasource) CTX.lookup (JNDI); Connection CN = ds.getConnection (); mostly used in JSP
2, execute SQL statement 1) Use statement to execute SQL statement string sql; statement SM = cn.createstatement (); sm.executeQuery (SQL); // Execute Data Query Schematic (SQL); // Perform a data update statement (delete, update, insert, drop, etc.) statement.close (); 2) Execute SQL statements String SQL; SQL = INSERT INTO USER (ID, NAME) VALUES (?); PreparedStateMent PS = cn.preparestatement (SQL); ps.setint (1, xxx); ps.setstring (2, xxx); ... resultset = ps.executeQuery (); // query int C = ps.executeUpdate ); // update 3, process the execution result query statement, return the record set RESULTSET update statement, return the number, indicating that the number of records affected by the update Resultset 1, next (), move the cursor to move, if successful returns true Otherwise returning false 2, getint ("id") or getSting ("name"), returns the value of a field under the current cursor 4, release connection cnclose (); general, turn off the resultTset first, then close Statement (or PreparedStatement); Finally close Connection
Scrollable, a set of updated records, create scrolling, updating Statement Statement sm = cn.createStatement (ResultSet.TYPE_SCROLL_ENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet Statement of the acquired when the designated parameter PreparedStatemet ps 2 is scrollable, create PreparedStatement = cn.prepareStatement (sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet.absolute (9000); batch update 1, Statement Statement sm = cn.createStatement (); sm.addBatch (sql1); sm.addBatch (sql2) ; ... sm.executebatch () A Statement object, after multiple SQL statements, batch updates. This multiple statements can be delete, update, insert, etc., PREPAREDSTATEMENT PREPAREDSTATEMENT PS = cn.preparedStatement (SQL); {ps.setxxx (1, xxx); ... ps.addbatch ();} PS. Executebatch (); a preparedStatement, you can execute a SQL statement, transform parameters multiple times, updated once.
Processing of the transaction 1, turn off the automatic submission of Connection CN.SetAutoCommit (false); 2, execute a series of SQL statements: Before performing each new SQL statement, the last time the SQL statement is executed, the SQL statement must be first closed Statement first. SM; SM = cn.createstatement (INSERT INTO User ...); sm.executeUpdate (); sm.close ();
SM = cn.createstatement ("INSERT INTO CORP ...); sm.executeUpdate (); sm.close ();
3, submit cn.commit (); 4, if an exception occurs, then rollback cn.rollback (); - Big Belon 2004.10.9