请教int nResult=st.executeUpdate(sql);问题

lcs_cs 2006-05-06 10:36:35
public static int executeSQL(Connection conn,String sql) throws SQLException
{
Statement st=conn.createStatement();
int nResult=st.executeUpdate(sql);
st.close();
return nResult;
}
我写了这样一个method但是每次,执行到int nResult=st.executeUpdate(sql);好像就不执行了(在调试的时候发现的),请教是怎么回事?
谢谢!
...全文
502 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
csbudong 2006-05-07
  • 打赏
  • 举报
回复
搞定拉
   可能是你这里int nResult=st.executeUpdate(sql);
   sql不是个更新语句
   它就会报出这样一个错误 N row count was produced

//test.java文件
package yumen;

import java.*;
import java.sql.*;

public class test
{

public test()
{

try
{
//加载驱动程序
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(Exception ec)
{
System.out.println("未能加载驱动程序!!!");
System.out.println(ec.getMessage());
}
}
public static int executeSQL(Connection conn,String sql) throws SQLException
{
int nResult=0;
try
{
Statement st=conn.createStatement();
nResult=st.executeUpdate(sql);
st.close();

}
catch(Exception ex)
{
System.out.println(ex.getMessage());
nResult=0;

}
return nResult;
}

}//end class test


//test.jsp页面


<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<%@ page import="yumen.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head>

<body>
<%
test obj=new test();
Connection connect=DriverManager.getConnection("jdbc:odbc:OlineBook","sa","sa");
String strQuery="update userdetails set Answer='cc' where username='budong'";
int i=obj.executeSQL(connect,strQuery);
System.out.println("收到:"+i);
%>
</body>
</html>


是可以运行的!!

csbudong 2006-05-07
  • 打赏
  • 举报
回复
执行到这里int nResult=st.executeUpdate(sql);他报拉什么错误啊!!
lcs_cs 2006-05-07
  • 打赏
  • 举报
回复
这个小问题没有人会吗?
lcs_cs 2006-05-07
  • 打赏
  • 举报
回复
第一个问题解决了帮忙看看第二个
panshengbin 2006-05-07
  • 打赏
  • 举报
回复
把nResult定义成ResultSet试一下
据我所知结果集(即nResult)是不能赋给int类型的变量
lcs_cs 2006-05-07
  • 打赏
  • 举报
回复
上面的是新的问题,是这样的,我要求,如果输入东西的话,就安输入的查询,如果不输入的话,就返回所有的值,现在只要我输入东西,就查询不到,就是有相配的也不可以,但是不输入的话,可以返回所有值,请教怎么回事?
lcs_cs 2006-05-07
  • 打赏
  • 举报
回复
<%@ page language="java" contentType="text/html; charset=GB2312"
pageEncoding="GB2312"%>
<%@ page import="com.lcs.pub.db.*" %>
<%@ page import="com.lcs.managebean.CategoryBean" %>
<%@ page import="com.lcs.category.Category" %>
<%@ page import="com.lcs.pub.util.StringUtil" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="category" class="com.lcs.category.Category" scope="page">
<jsp:setProperty name="category" property="*" />
</jsp:useBean>
<jsp:useBean id="categorybean" class="com.lcs.managebean.CategoryBean" scope="page"></jsp:useBean>
<%
java.util.Vector categorys = categorybean.querycategory(category);
if (categorys==null || categorys.isEmpty())
{
out.print(StringUtil.getStr("sorry"));
}
else
{
if (StringUtil.isEmptyString(category.getName()))
out.print((StringUtil.getStr(category.getName())).length());
for(int i=0;i<categorys.size();i++)
{
Category c=(Category)categorys.get(i);
String s=new String(c.getName().getBytes("GBK"));
out.print(s);
out.print(" <a href='category_updatein.jsp?Categoryid="+c.getCategoryid()
+"'>修改</a> ");
out.print("<a href='category_del.jsp?Categoryid="+c.getCategoryid()
+"'>删除</a>");
}
}
%>
</body>
</html>
//查询商品分类信息
public Vector querycategory(Category category)
{
Vector vt=new Vector();
Connection conn=null;
try
{
conn=DatebaseBean.getConnection();
String sql="select * from category";
//如果用户输入了分类名称,就采用模糊查询
if ( !StringUtil.isEmptyString(category.getName()))
{
sql=sql+"where name like '%"+category.getName()+"%'";
//增加排序规则
sql=sql+" order by categoryid desc";
}
ResultSet rs=DatebaseBean.executeSearchSQL(conn,sql);
while (rs.next())
{
//得到查询结果
Category cate=new Category();
cate.setCategoryid(rs.getInt(1));
cate.setName(rs.getString(2));
vt.add(cate);
}
rs.close();
}
catch(SQLException ex)
{
ex.printStackTrace();
}
finally
{//释放资源
DatebaseBean.close(null,null,conn);
}
return vt;
} //完成查找工作并返回数据集合
public static ResultSet executeSearchSQL(Connection conn,String sql) throws SQLException
{
Statement st=conn.createStatement();
ResultSet rs=st.executeQuery(sql);
return rs;
}
coolbamboo2008 2006-05-07
  • 打赏
  • 举报
回复
不可以!只能增、删、改!
lcs_cs 2006-05-07
  • 打赏
  • 举报
回复
查询语句不可以吗?

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧