1. Java database operation Basic process 2, several commonly used important techniques: can scroll, updated record set batch update transaction Java database operation Basic flow: get database connection - execute SQL statement - Processing execution results - Release Database connection 1, Get Database Connection 1) Take the Database Connection Example by DriverManager:
String ClassName, URL, UID, PWD; ClassName = "Oracle.jdbc.driver.Orcledriver"; 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) An example of using JNDI (Java Naming and Directory Service)
String JNDI = "JDBC / DB"; Context CTX = (Context) New InitialContext (). Lookup ("Java: Comp / ENV"); DataSource DS = (Datasource) CTX.lookup (JNDI); connection cn = ds.getConnection (); Multi-use in JSP 2, execute SQL statement 1) Execute SQL statements with Statement
String SQL; Statement SM = cn.createStatement (); sm.executeQuery (SQL); // Execute data query statement (SELECT) sm.executeUpdate (SQL); // Perform data update statement (delete, update, insert, drop, etc. ), Statement.close (); 2) execute SQL statements with preparedStatement
String SQL; SQL = "INSERT INTO USER (ID, NAME) VALUES (?,?)"; PreparedState ps = cn.prepareStatement (SQL); ps.setint (1, xxx); ps.setstring (2, xxx); ... ResultSet RS = ps.executeQuery (); // Query INT C = ps.executeUpdate (); // Update 3, processing execution results query statement, return to record set ResultSet. Update the statement, returns the number, indicating the number of records affected by the update. ResultSet Method: 1, next (), move the cursor to the back, if you successfully return true; otherwise return false. 2, getint ("ID") or getSting ("name"), returns the value of a field under the current cursor. 3, release the connection.
Cn.close (); general, turn off the resultSet first, then close the statement (or preparedStatement); finally close the Connection can scroll, update the recordset 1, create scrollable, updated Statement
Statement sm = cn.createStatement (ResultSet.TYPE_SCROLL_ENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet the Statement made upon specified parameters 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. These multiple statements can be delete, update, insert, etc., or 2, preparedStatement
PreparedState ps = cn.preparedStatement (SQL); {ps.setxxx (1, xxx); ... ps.addbatch ();} ps.executebatch (); a preparedStatement, you can put a SQL statement, transform parameters multiple times Once updated once. Processing of the transaction 1. Close the automatic submission of Connection
CN.SetAutoCommit (False); 2, execute a series of SQL statement points: Before performing each new SQL statement, the last execution of SQL statements must be closed first.
STATEMENT 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 ();