为什么我在这个地方无法将connection的autocommit关闭呢?

zrc 发布于 2014/08/26 09:52
阅读 122
收藏 0
package org.JDBCYinyong;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;


public class ConntactDBtest {


public static Connection TestConnecthin() throws ClassNotFoundException,
SQLException {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8";
Connection con = DriverManager.getConnection(url, "root", "root");
return con;


}
}
package org.JDBCYinyong;


import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class TestSelect {
public TestSelect() throws SQLException, ClassNotFoundException {
Statement st = ConntactDBtest.TestConnecthin().createStatement();
ConntactDBtest.TestConnecthin().setAutoCommit(false);
String sql = "update A set id = 'xxxxx' where a = 'aaaa' ";
String sql1 = "update B set id = 'xxxxx' where b = 'bbbb'";
int sqlA = st.executeUpdate(sql);
int sqlB = st.executeUpdate(sql1);
if (sqlA == 1 && sqlB == 1) {
ConntactDBtest.TestConnecthin().commit();
} else {
ConntactDBtest.TestConnecthin().rollback();
}


}


public static void main(String[] args) throws SQLException,
ClassNotFoundException {
// TODO Auto-generated method stub
TestSelect a = new TestSelect();
}


}


加载中
0
瞌睡来了
瞌睡来了
conn = ConntactDBtest.TestConnecthin();
conn.setAutoCommit(false);
Statement st = conn.createStatement();

这样的吧

0
lock_free
lock_free

敢情你每次都取新的conn ,能有用吗?

0
zrc
zrc
谢谢,懂了。。。
返回顶部
顶部