求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(); } } }} java怎么生成距离相等的点 java处理数组问题 有谁能够将读入的字符串中\,转化成\\ C#中的IDisposable接口,在java中是哪个! 刚入门者的问题 BigDecimal是做什么的? 连接SQL server 2000数据库问题 java 反编译器 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();
}
}
}
}