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);好像就不执行了(在调试的时候发现的),请教是怎么回事?
谢谢!
{
Statement st=conn.createStatement();
int nResult=st.executeUpdate(sql);
st.close();
return nResult;
}
我写了这样一个method但是每次,执行到int nResult=st.executeUpdate(sql);好像就不执行了(在调试的时候发现的),请教是怎么回事?
谢谢!
可能是你这里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>
是可以运行的!!
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;
}
据我所知结果集(即nResult)是不能赋给int类型的变量