<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<html xmlns="">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>事务处理程序</title>
</head>
<body>
<table width="372" border="1" align="center" cellspacing="0">
<tr>
<td><div align="center">事务处理程序</div></td>
</tr>
<tr>
<td>
<%
String sqlString1="update userinfo set age=21 where name like '12%'";
String sqlString2="update userinfo set age=22 where name like '吴%'";
//String sqlString1="select userinfo where name like '冼%'";
//String sqlString2="select userinfo where name like '吴%'";
Connection con;
Statement sql;
// ResultSet rs;
//String sqlString;
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
con=DriverManager.getConnection("jdbc:microsoft:sqlserver://MICROSOF-BJER9M:1433;DatabaseName=basesql","sa","12345");
try
{
con.setAutoCommit(false);
sql=con.createStatement();
sql.executeUpdate(sqlString1);
sql.executeUpdate(sqlString2);
con.commit();
out.print("事务提交成功,执行两条SQL语句!");
con.close();
}
catch(SQLException e)
{
con.rollback();
out.print("事务操作不成功,事务已回滚!");
}
%>
</td>
</tr>
</table>
</body>
</html>
我的数据库中只有两条记录,一条为冼某某(name),21(age),另一条为吴某某(name),22(age)
为什么我执行的时候,输出的结果"是事务提交成功,执行两条SQL语句!"
我原以为是"事务操作不成功,事务已回滚!",因为我的记录中并没有12**的记录,谁能解释一下?
[此贴子已经被作者于2006-8-14 9:38:20编辑过]
----------------解决方案--------------------------------------------------------
怎么没人理我?
----------------解决方案--------------------------------------------------------
不好意思,偶休假~哈哈
update找不到指定的记录会抛出异常么?呵呵,再想想
----------------解决方案--------------------------------------------------------
恩,准确说,应该是没有记录和update的where条件匹配
----------------解决方案--------------------------------------------------------
对呀,只有一条记录匹配,另一条记录不匹配
而事务就是要两条记录都匹配才输出"事务提交成功,执行两条SQL语句!
为何不匹配的情况下也输出上面这一句话呢??
这就是我想要问的问题
----------------解决方案--------------------------------------------------------
你认为找不到数据,数据库会发生异常么?
我是认为不会发生
----------------解决方案--------------------------------------------------------
只要不发生异常就会输出事务成功,显然,你的sql没写错,所以不会发生异常,顶多是没有记录而已
----------------解决方案--------------------------------------------------------