Several common techniques in Java database programming

xiaoxiao2021-03-06  40

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) Examples of ways with JNDI (Java Names and Directory Services)

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 statements 1) Use statement to perform SQL statements

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. );

2) Use preparedStatement to perform SQL statements

String SQL; SQL = "INSERT INTO USER (ID, NAME) VALUES (?,?)"; PreparedState 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 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 and then close the statement (or preparedStatement); finally close the connection can scroll, update the record set 1, create scrollable, updated Statement

Statement SM = cn.createStatement (ResultSet.Type_Scroll_ensitive, ResultSet.concur_read_only);

The Statement made ResultSet is scrollable 2, specify when you create a PreparedStatement parameter PreparedStatemet ps = 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 execute a SQL statement, transform parameters multiple times, updated once. Processing of the transaction 1. Close 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 (or preparedStateTate) first Close 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 roll back

Cn.rollback ();

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

New Post(0)