关于SQL连接
我的代码是:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'delete.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="" method="get">
选择删除的方式: <label>
<input type="radio" name="deltype" value="按读者编号删除" />
按读者编号删除</label><label>
<input type="radio" name="deltype" value="按姓名删除" />按姓名删除
</label>
<label> <input type="text" name="delinfo" />
</label><label>
<input type="submit" name="Submit" value="提交删除" />
</label>
<td class="STYLE3"><div align="right"><a href="main.jsp" class="STYLE4 STYLE1"><kbd>返回首页</kbd></a></div></td>
</form> <%
String deltype = request.getParameter("deltype");
try{
byte d[] = deltype.getBytes("ISO-8859-1");
deltype = new String(d);}
catch(Exception e){}
String delinfo=request.getParameter("delinfo");
try{ byte b[]=delinfo.getBytes("ISO-8859-1");
delinfo=new String(b);}
catch(Exception e){}
if(deltype!=null&&delinfo!=null){
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String strConn= "jdbc:sqlserver://localhost;databaseName=zc;user=sa;password=123";
Connection conn= DriverManager.getConnection(strConn);
Statement stmt=conn.createStatement();;
String userid="按读者编号删除";
String username ="按姓名删除";
String a=userid.trim();
String b=deltype.trim();
if((userid.trim()).equals(deltype.trim())){
String sql1 ="delete from reader where 读者编号='"+delinfo+"'";
int i1 = stmt.executeUpdate(sql1);
out.print("删除成功!!!");}
if((username.trim()).equals(deltype.trim())){
String sql2 ="delete from reader where 读者姓名='"+delinfo+"'";
int i2 = stmt.executeUpdate(sql2);
out.print("删除成功!!!");}}%>
</body>
</html>
结果报错:严重: Servlet.service() for servlet jsp threw exception
com.microsoft.sqlserver.jdbc.SQLServerException: 该语句已关闭。
请求高人指点
[解决办法]
你把倒数第六行开始的
if((username.trim()).equals(deltype.trim())){
String sql2 ="delete from reader where 读者姓名='"+delinfo+"'";
int i2 = stmt.executeUpdate(sql2);
out.print("删除成功!!!");}}
这段代码删除,看看还有没有报这个错误
[解决办法]
执行SQL的部分 替换为下面的代码,然后sql1 sql2打印出来看看有没有乱码的情况
如果没有乱码的情况你直接在查询分析器里面执行,看看能删除数据么
if((userid.trim()).equals(deltype.trim())){
String sql1 ="delete from reader where 读者编号='"+delinfo+"'";
System.out.println(sql1);
int i1 = stmt.executeUpdate(sql1);
out.print("删除成功!!!");}
if((username.trim()).equals(deltype.trim())){
String sql2 ="delete from reader where 读者姓名='"+delinfo+"'";
System.out.println(sql2);
int i2 = stmt.executeUpdate(sql2);
out.print("删除成功!!!");}}
我的异常网推荐解决方案:Servlet.service() for servlet default threw exception,http://www.myexception.cn/eclipse/181756.html