求java写的mssql事务例子。 我用java加mssql数据库做项目时需要同时插入二个以上记录(二个以上表里面新增记录)。如果不用事务(Transaction)的话麻烦。 我需要一个例子。请大家帮帮忙。我先谢谢大家了。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import java.sql.*;public class Test{ public static void main(String[] args) { Connection ct = null; Statement sm = null; try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); ct = DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1433;databaseName=spdb1;user=sa;password=sa;"); sm = ct.createStatement(); sm.executeUpdate("BEGIN TRANSACTION"); sm.executeUpdate("update users set grade = grade + 1 where username='tester11'"); int a = 1 / 0; sm.executeUpdate("update users set grade = grade - 1 where username='tester12'"); sm.executeUpdate("COMMIT TRANSACTION"); } catch(Exception ex) { try { sm.executeUpdate("ROLLBACK TRANSACTION"); } catch(Exception innerEx) { innerEx.printStackTrace(); } ex.printStackTrace(); } finally { try { sm.close(); ct.close(); } catch(Exception ex) { ex.printStackTrace(); } } }} jdbc的事务和数据库没有关系。try{connection.setAutoCommit(false);操作1操作2....然后执行完操作,进行commit就行了。}catch(){ //回滚}在finally中然后把手动提交setAutoCommit(true); import java.sql.*;public class Test{ public static void main(String[] args) { Connection ct = null; Statement sm = null; try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); ct = DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1433;databaseName=spdb1;user=sa;password=sa;"); sm = ct.createStatement(); ct.setAutoCommit(false); for(int i=0;i<10;i++) { sm.executeUpdate("insert into table values('''''''''')"); } ct.commit(); } catch(Exception ex) { try { ct.rollback(); } catch(Exception innerEx) { innerEx.printStackTrace(); } ex.printStackTrace(); } finally { try { sm.close(); ct.close(); } catch(Exception ex) { ex.printStackTrace(); } } }} import java.sql.*;public class Test{ public static void main(String[] args) { Connection ct = null; Statement sm = null; try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); ct = DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1433;databaseName=spdb1;user=sa;password=sa;"); sm = ct.createStatement(); ct.setAutoCommit(false); for(int i=0;i<10;i++) { sm.executeUpdate("insert into table values('''''''''')"); } ct.commit(); ct.setAutoCommit(true); } catch(Exception ex) { try { ct.rollback(); } catch(Exception innerEx) { innerEx.printStackTrace(); } ex.printStackTrace(); } finally { try { sm.close(); ct.close(); } catch(Exception ex) { ex.printStackTrace(); } } }} 初学者的小问题,麻烦指教下---->为什么PrintStream一定要定义为属性 数据库设计 为什么我用hibernate连接SQL 2005 他报错 组件重绘 求一最高效的方式,遍历,修改,删除一个ArrayList中的若干元素? 为什么JFrame中只能显示一个控件? 请问java中如何将带符号的浮点数转换为字符串? 初级问题! getName()==null ???? 急! 帮JAVA新人看看! java初学,一个关于socket的比较困惑的问题 帮忙解决一下下面的异常,先谢谢了!
{
public static void main(String[] args)
{
Connection ct = null;
Statement sm = null;
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
ct = DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1433;databaseName=spdb1;user=sa;password=sa;");
sm = ct.createStatement();
sm.executeUpdate("BEGIN TRANSACTION");
sm.executeUpdate("update users set grade = grade + 1 where username='tester11'");
int a = 1 / 0;
sm.executeUpdate("update users set grade = grade - 1 where username='tester12'");
sm.executeUpdate("COMMIT TRANSACTION");
}
catch(Exception ex)
{
try
{
sm.executeUpdate("ROLLBACK TRANSACTION");
}
catch(Exception innerEx)
{
innerEx.printStackTrace();
}
ex.printStackTrace();
}
finally
{
try
{
sm.close();
ct.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
}
try
{connection.setAutoCommit(false);
操作1
操作2
....然后执行完操作,
进行commit就行了。
}catch(){
//回滚
}
在finally中然后把手动提交setAutoCommit(true);
{
public static void main(String[] args)
{
Connection ct = null;
Statement sm = null;
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
ct = DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1433;databaseName=spdb1;user=sa;password=sa;");
sm = ct.createStatement();
ct.setAutoCommit(false);
for(int i=0;i<10;i++) {
sm.executeUpdate("insert into table values('''''''''')");
}
ct.commit();
}
catch(Exception ex)
{
try
{
ct.rollback();
}
catch(Exception innerEx)
{
innerEx.printStackTrace();
}
ex.printStackTrace();
}
finally
{
try
{
sm.close();
ct.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
}
{
public static void main(String[] args)
{
Connection ct = null;
Statement sm = null;
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
ct = DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1433;databaseName=spdb1;user=sa;password=sa;");
sm = ct.createStatement();
ct.setAutoCommit(false);
for(int i=0;i<10;i++) {
sm.executeUpdate("insert into table values('''''''''')");
}
ct.commit();
ct.setAutoCommit(true); }
catch(Exception ex)
{
try
{
ct.rollback();
}
catch(Exception innerEx)
{
innerEx.printStackTrace();
}
ex.printStackTrace();
}
finally
{
try
{
sm.close();
ct.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
}