关键代码:
stmt = conn.createStatement();
conn.setAutoCommit(false);/*注意这里,下面没有使用conn.commit()*/
stmt.addBatch("insert into t_one values( 6, 'eee' )");
stmt.addBatch("insert into t_one values( 7, 'eee' )");
stmt.addBatch("insert into t_one values( 8, 'eee' )");
stmt.addBatch("insert into t_one values( 9, 'eee' )");
stmt.executeBatch();
在没有使用conn.commit()的情况下,为什么执行完这些语句数据还是会加入表?
也就是说在执行完上面语句后能够通过其他用户登陆数据库查到这些数据完全代码
import java.sql.*;public class TestDML
{ public static void main(String[] args) throws ClassNotFoundException , SQLException , Exception
{
Connection conn = null;
Statement stmt = null;
try
{
Class.forName( "oracle.jdbc.driver.OracleDriver" );
//注册Oracle
conn = DriverManager.getConnection( "jdbc:oracle:thin:@127.0.0.1:1521:NODB" , "bbs" , "bbs" );
//得到Oracle的连接
stmt=conn.createStatement();
conn.setAutoCommit(false);
stmt.addBatch("{call p(6,'eee')}");
stmt.addBatch("{call p(7,'eee')}");
stmt.addBatch("{call p(8,'eee')}");
stmt.addBatch("{call p(9,'eee')}");
stmt.executeBatch();
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
catch(SQLException e)
{
e.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try
{//将打开的东西关掉:如
if ( stmt != null )
{
stmt.close();
stmt = null;
}
if ( conn != null )
{
conn.close();
conn = null;
}
}
catch(SQLException e)
{
e.printStackTrace();
}
}
}
}
stmt = conn.createStatement();
conn.setAutoCommit(false);/*注意这里,下面没有使用conn.commit()*/
stmt.addBatch("insert into t_one values( 6, 'eee' )");
stmt.addBatch("insert into t_one values( 7, 'eee' )");
stmt.addBatch("insert into t_one values( 8, 'eee' )");
stmt.addBatch("insert into t_one values( 9, 'eee' )");
stmt.executeBatch();
在没有使用conn.commit()的情况下,为什么执行完这些语句数据还是会加入表?
也就是说在执行完上面语句后能够通过其他用户登陆数据库查到这些数据完全代码
import java.sql.*;public class TestDML
{ public static void main(String[] args) throws ClassNotFoundException , SQLException , Exception
{
Connection conn = null;
Statement stmt = null;
try
{
Class.forName( "oracle.jdbc.driver.OracleDriver" );
//注册Oracle
conn = DriverManager.getConnection( "jdbc:oracle:thin:@127.0.0.1:1521:NODB" , "bbs" , "bbs" );
//得到Oracle的连接
stmt=conn.createStatement();
conn.setAutoCommit(false);
stmt.addBatch("{call p(6,'eee')}");
stmt.addBatch("{call p(7,'eee')}");
stmt.addBatch("{call p(8,'eee')}");
stmt.addBatch("{call p(9,'eee')}");
stmt.executeBatch();
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
catch(SQLException e)
{
e.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try
{//将打开的东西关掉:如
if ( stmt != null )
{
stmt.close();
stmt = null;
}
if ( conn != null )
{
conn.close();
conn = null;
}
}
catch(SQLException e)
{
e.printStackTrace();
}
}
}
}
解决方案 »
- Enterprise Manager 无法连接到数据库实例
- 请教一个删除视图数据的问题
- 数据库备份如何exclude某些表
- 请问oracle的异机数据库恢复条件~!
- 关于查询的问题,有点麻烦。请高手赐教!
- 我的ORACLE9.2 服务器在有应用程序在访问的情况下断电了,无法进入了。
- 如何获取表数据
- Oracle中char类型的字段可以进行date运算吗?
- 大侠救我!linux下装oracle920的时候报这个错误,安装窗口中的字符是乱码!
- 在plsql的sqlwindow中执行语句块 execute immediate 'select * from tmp_rep'没有显示查询结果
- 在删除表前,oracle如何判断表是否存在,存在就删再创建,不存在直接创建!
- ORA-12170:TNS:连接超时
相当于insert into t_one values( 6 , 'eee' );
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;public class Test {
// 数据库驱动
private final static String DATABASE_DRIVER = "oracle.jdbc.driver.OracleDriver";
// 数据源URL
private final static String DATABASE_SOURCE = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
// 登录用户
private final static String USER_NAME = "scott";
// 用户密码
private final static String PASSWORD = "tiger";
public static void main(String[] args){
Connection cn = null;
try {
// 加载驱动
Class.forName(DATABASE_DRIVER);
// 创建连接
cn = DriverManager.getConnection(DATABASE_SOURCE, USER_NAME, PASSWORD);
// 设置状态
Statement st = cn.createStatement();
// 不自动提交
cn.setAutoCommit(false);
// DML语句
st.executeUpdate("update emp set sal = sal + 100 where ename = 'KING'");
// 制造异常
// int var = 1/0;
// 执行脚本
ResultSet rs = st.executeQuery("select ename, sal from emp where ename = 'KING'");
// 非空循环
while (rs.next()) {
System.out.println("姓名=" + rs.getString(1) + " 工资=" + rs.getString(2));
}
// 统一提交
// cn.commit();
// 关闭资源
rs.close();
st.close();
cn.close();
} catch (Exception e) {
// 回滚事务
try {
cn.rollback();
} catch (Exception ex) {
ex.printStackTrace();
}
// 输出异常
e.printStackTrace();
}
}
}
此时没有cn.commit();但结果却依然执行提交?