注册 登录
编程论坛 J2EE论坛

[求助]事务的问题

zehua 发布于 2006-08-14 00:31, 641 次点击

<%@ 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编辑过]

6 回复
#2
zehua2006-08-14 10:46
怎么没人理我?
#3
神vLinux飘飘2006-08-14 18:17
不好意思,偶休假~哈哈

update找不到指定的记录会抛出异常么?呵呵,再想想
#4
神vLinux飘飘2006-08-14 18:18
恩,准确说,应该是没有记录和update的where条件匹配
#5
zehua2006-08-14 23:16

对呀,只有一条记录匹配,另一条记录不匹配
而事务就是要两条记录都匹配才输出"事务提交成功,执行两条SQL语句!
为何不匹配的情况下也输出上面这一句话呢??
这就是我想要问的问题

#6
神vLinux飘飘2006-08-16 19:54
你认为找不到数据,数据库会发生异常么?
我是认为不会发生
#7
飘飘叶子2006-08-16 20:02

只要不发生异常就会输出事务成功,显然,你的sql没写错,所以不会发生异常,顶多是没有记录而已

1