Oracle JDBC
2012-04-15 18:04
49 查看
1、CONNECT
The “Statement” interface is used to execute a simple SQL statement with no parameters. For create, insert, update or delete statement, uses “
3、 To issue a create statement, calls the S
4、To issue a insert statement, calls the
5、To issue a update statement, calls the
6、 To issue a delete statement, calls the
7、 To issue a select query, calls the
8、批量处理数据;
Here’s an example to show you how to insert few records in batch process, via JDBC
Batch Update is not limit to Insert statement, it’s apply for Update and Delete statement as well.
insertSQL
JAVA:
JDBC & PreparedStatement
The “PreparedStatement” interface is extended “Statement”, with extra feature to send a pre-compiled SQL statement with parameters. For create, insert, update or delete statement, uses “
1、To issue a create statement, calls the
2、To issue an insert statement, calls the
3、To issue a update statement, calls the
4、To issue a delete statement, calls the
5、display the records via a ResultSet object. To issue a select query, calls the
6、批量处理数据;Batch Update is not limit to Insert statement, it’s apply for Update and Delete statement as well.(适用于增删改)
JDBC事务管理:
JDBC Transaction let you control how and when a transaction should commit into database.
In simple, JDBC transaction make sure SQL statements within a transaction block are all executed successful, if either one of the SQL statement within transaction block is failed, abort and rollback everything within the transaction block.
(JDBC确保了在事务块中的所有语句都执行成功,如果它们之中的一个执行失败,那么在事务中执行的所有操作都会进行回滚)
(默认情况下,数据将会在执行executeUpdate()方法的时候,提交到数据库中):
When this code is executed, the USER_ID = ’999′ is inserted but the username is not update.
(数据提交到数据库中去,但是后面的更新语句失败)
When this code is executed, update statement is hits error, and make both insert and update statements rollback together.(所有的操作都会进行回滚)
Class.forName("org.postgresql.Driver"); Connection connection = null; connection = DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:mkyong","username","password"); connection.close();
JDBC & Statement
2、STATAMENTThe “Statement” interface is used to execute a simple SQL statement with no parameters. For create, insert, update or delete statement, uses “
Statement.executeUpdate(sql)“; select query, uses “
Statement.executeQuery(sql)“.
3、 To issue a create statement, calls the S
tatement.execute()method like this :
Statement statement = dbConnection.createStatement(); // execute create SQL stetement statement.execute(createTableSQL);
4、To issue a insert statement, calls the
Statement.executeUpdate()method like this :
Statement statement = dbConnection.createStatement(); // execute the insert SQL stetement statement.executeUpdate(insertTableSQL);
5、To issue a update statement, calls the
Statement.executeUpdate()method like this :
Statement statement = dbConnection.createStatement(); // execute the update SQL stetement statement.executeUpdate(updateTableSQL);
6、 To issue a delete statement, calls the
Statement.executeUpdate()method like this :
Statement statement = dbConnection.createStatement(); // execute the delete SQL stetement statement.executeUpdate(deleteTableSQL);
7、 To issue a select query, calls the
Statement.executeQuerymethod like this :
String selectTableSQL = "SELECT USER_ID, USERNAME from DBUSER"; Statement statement = dbConnection.createStatement(); ResultSet rs = statement.executeQuery(selectTableSQL); while (rs.next()) { String userid = rs.getString("USER_ID"); String username = rs.getString("USERNAME"); }
8、批量处理数据;
Here’s an example to show you how to insert few records in batch process, via JDBC
Statement.
Batch Update is not limit to Insert statement, it’s apply for Update and Delete statement as well.
insertSQL
String insertTableSQL1 = "INSERT INTO DBUSER" + "(USER_ID, USERNAME, CREATED_BY, CREATED_DATE) " + "VALUES" + "(101,'mkyong','system', " + "to_date('" + getCurrentTimeStamp() + "', 'yyyy/mm/dd hh24:mi:ss'))";
JAVA:
dbConnection.setAutoCommit(false); statement = dbConnection.createStatement(); 4 statement.addBatch(insertTableSQL1); 5 statement.addBatch(insertTableSQL2); 6 statement.addBatch(insertTableSQL3); statement.executeBatch(); dbConnection.commit();
JDBC & PreparedStatement
The “PreparedStatement” interface is extended “Statement”, with extra feature to send a pre-compiled SQL statement with parameters. For create, insert, update or delete statement, uses “
PreparedStatement.executeUpdate(sql)“; select query, uses “
PreparedStatement.executeQuery(sql)“.
1、To issue a create statement, calls the
PrepareStatement.executeUpdate()method like this :
PreparedStatement preparedStatement = dbConnection.prepareStatement(createTableSQL); // EXECUTE CREATE SQL stetement preparedStatement.executeUpdate();
2、To issue an insert statement, calls the
PreparedStatement.executeUpdate()method like this :
String insertTableSQL = "INSERT INTO DBUSER" + "(USER_ID, USERNAME, CREATED_BY, CREATED_DATE) VALUES" + "(?,?,?,?)"; PreparedStatement preparedStatement = dbConnection.prepareStatement(insertTableSQL); preparedStatement.setInt(1, 11); preparedStatement.setString(2, "mkyong"); preparedStatement.setString(3, "system"); preparedStatement.setTimestamp(4, getCurrentTimeStamp()); // execute insert SQL stetement preparedStatement .executeUpdate();
3、To issue a update statement, calls the
PreparedStatement.executeUpdate()method like this :
String updateTableSQL = "UPDATE DBUSER SET USERNAME = ? WHERE USER_ID = ?"; PreparedStatement preparedStatement = dbConnection.prepareStatement(updateTableSQL); preparedStatement.setString(1, "mkyong_new_value"); preparedStatement.setInt(2, 1001); // execute update SQL stetement preparedStatement .executeUpdate();
4、To issue a delete statement, calls the
PreparedStatement.executeUpdate()method like this :
String deleteSQL = "DELETE DBUSER WHERE USER_ID = ?"; PreparedStatement preparedStatement = dbConnection.prepareStatement(deleteSQL); preparedStatement.setInt(1, 1001); // execute delete SQL stetement preparedStatement.executeUpdate();
5、display the records via a ResultSet object. To issue a select query, calls the
PreparedStatement.executeQuery()method like this
String selectSQL = "SELECT USER_ID, USERNAME FROM DBUSER WHERE USER_ID = ?"; PreparedStatement preparedStatement = dbConnection.prepareStatement(selectSQL); preparedStatement.setInt(1, 1001); ResultSet rs = preparedStatement.executeQuery(selectSQL ); while (rs.next()) { String userid = rs.getString("USER_ID"); String username = rs.getString("USERNAME"); }
6、批量处理数据;Batch Update is not limit to Insert statement, it’s apply for Update and Delete statement as well.(适用于增删改)
dbConnection.setAutoCommit(false);//commit trasaction manually String insertTableSQL = "INSERT INTO DBUSER" + "(USER_ID, USERNAME, CREATED_BY, CREATED_DATE) VALUES" + "(?,?,?,?)"; PreparedStatement = dbConnection.prepareStatement(insertTableSQL); preparedStatement.setInt(1, 101); preparedStatement.setString(2, "mkyong101"); preparedStatement.setString(3, "system"); preparedStatement.setTimestamp(4, getCurrentTimeStamp()); preparedStatement.addBatch(); preparedStatement.setInt(1, 102); preparedStatement.setString(2, "mkyong102"); preparedStatement.setString(3, "system"); preparedStatement.setTimestamp(4, getCurrentTimeStamp()); preparedStatement.addBatch(); 19 preparedStatement.executeBatch(); dbConnection.commit();
JDBC事务管理:
JDBC Transaction let you control how and when a transaction should commit into database.
//transaction block start //SQL insert statement //SQL update statement //SQL delete statement //transaction block end
In simple, JDBC transaction make sure SQL statements within a transaction block are all executed successful, if either one of the SQL statement within transaction block is failed, abort and rollback everything within the transaction block.
(JDBC确保了在事务块中的所有语句都执行成功,如果它们之中的一个执行失败,那么在事务中执行的所有操作都会进行回滚)
1. Without JDBC Transaction
By default, data will be committed into database whenexecuteUpdate()is called.
(默认情况下,数据将会在执行executeUpdate()方法的时候,提交到数据库中):
String insertTableSQL = "INSERT INTO DBUSER" + "(USER_ID, USERNAME, CREATED_BY, CREATED_DATE) VALUES" + "(?,?,?,?)"; String updateTableSQL = "UPDATE DBUSER SET USERNAME =? " + "WHERE USER_ID = ?"; preparedStatementInsert = dbConnection.prepareStatement(insertTableSQL); preparedStatementInsert.setInt(1, 999); preparedStatementInsert.setString(2, "mkyong101"); preparedStatementInsert.setString(3, "system"); preparedStatementInsert.setTimestamp(4, getCurrentTimeStamp()); preparedStatementInsert.executeUpdate(); //data COMMITTED into database. preparedStatementUpdate = dbConnection.prepareStatement(updateTableSQL); preparedStatementUpdate.setString(1, "A very very long string caused DATABASE ERROR"); preparedStatementUpdate.setInt(2, 999); preparedStatementUpdate.executeUpdate(); //Error, value too big, ignore this update statement, //but user_id=999 is inserted(出错)
When this code is executed, the USER_ID = ’999′ is inserted but the username is not update.
(数据提交到数据库中去,但是后面的更新语句失败)
2. With JDBC Transaction
To put this in a transaction, you can use1、dbConnection.setAutoCommit(false);to start a transaction block.(开始事务)
2、dbConnection.commit();to end a transaction block.(结束事务)
dbConnection.setAutoCommit(false); //transaction block start(手动的控制事务) String insertTableSQL = "INSERT INTO DBUSER" + "(USER_ID, USERNAME, CREATED_BY, CREATED_DATE) VALUES" + "(?,?,?,?)"; String updateTableSQL = "UPDATE DBUSER SET USERNAME =? " + "WHERE USER_ID = ?"; preparedStatementInsert = dbConnection.prepareStatement(insertTableSQL); preparedStatementInsert.setInt(1, 999); preparedStatementInsert.setString(2, "mkyong101"); preparedStatementInsert.setString(3, "system"); preparedStatementInsert.setTimestamp(4, getCurrentTimeStamp()); preparedStatementInsert.executeUpdate(); //data IS NOT commit yet(数据当前还没有提交) preparedStatementUpdate = dbConnection.prepareStatement(updateTableSQL); preparedStatementUpdate.setString(1, "A very very long string caused DATABASE ERROR"); preparedStatementUpdate.setInt(2, 999); preparedStatementUpdate.executeUpdate(); //Error, rollback, including the first insert statement. (出现错误,所有的数据操作都进行回滚) dbConnection.commit(); //transaction block end(手动的提交事务)
When this code is executed, update statement is hits error, and make both insert and update statements rollback together.(所有的操作都会进行回滚)
相关文章推荐
- Oracle JDBC连接数据库
- Oracle JDBC内存管理--Oracle白皮书2009年8月 .
- spring 配置oracle jdbc
- Oracle JDBC 连接时的一个BUG
- Oracle JDBC存入图片Blob
- Oracle JDBC存入图片Blob
- Oracle JDBC存入图片Blob
- Oracle JDBC2.0 数据来源(Data Source)与连接池(Connection Pool)
- oracle JDBC 链接查询
- Oracle Jdbc的一些限制(10.2.0.1)
- Oracle JDBC 连接卡死后 Connection Reset
- oracle JDBC
- Oracle JDBC通过占位符可以查询可变长字段,不可查询固定长度字段
- Oracle JDBC 11gR1的一些特性
- Oracle JDBC通过占位符可以查询可变长字段,不可查询固定长度字段
- Oracle jdbc
- Oracle jdbc 插入 clob blob
- Oracle JDBC 自带连接池
- oracle jdbc连接报错:违反协议
- ORACLE JDBC的getGeneratedKeys