主题:jsp连接数据库问题 大虾帮忙啊!
这里有个javabean的文件:LZ.java
package connect;
import javax.servlet.http.*;
import java.sql.*;
public class LZ implements HttpSessionBindingListener{
Connection con;
Statement sta;
ResultSet rs;
public void valueBound(HttpSessionBindingEvent event){}
public void valueUnbound(HttpSessionBindingEvent event){}
public LZ()
{
BulidConnection();
}
private void BulidConnection()
{
try
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
System.out.println("driver is ok");
con = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName =bysj","sa","xfl039");
System.out.println("conection is ok");
}catch(Exception e)
{
e.printStackTrace();
}
}
public Connection getConnection()
{
if(con==null)
BulidConnection();
return con;
}
}
然后我用它连接数据库作了一个登陆的界面denglu.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<html>
<head>
<base href="<%=basePath%>">
<title>登陆界面</title>
</head>
<body><br>
<center><font size=8>登录界面</font>
<br>
<br>
<%
String info=request.getParameter("info");
if(("1").equals(info))
out.println("<font size=4 color='red'>用户名错误或者不存在,请注册新用户!</font><br><br><br>");
else if(("2").equals(info))
out.println("<font size=4 color='red'>密码不正确,请重新登陆!</font><br><br><br>");
else
out.println("<font size=4 color='red'>请登陆!</font><br><br><br>");
%>
<br>
<br>
<form method='post' action="do_denglu.jsp"><div align="center"><div align="center">
用户名 <input name="username" maxlength="12" "onkeyup="value=value.replace(/[\W]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"><br></div><div align="left"> <br><div align="center">
密码 <input name="password" type="password" maxlength="12" onkeyup="value=value.replace(/[^\d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))">
</div></div></div>
<br>
<div align="center"><input type="submit" name="denglu" value="登陆">
<a href="./zcxx.jsp">新用户注册</a></div>
</form>
</body>
</html>
接着是确认登录的jsp:do_denglu.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ page import="java.sql.*"%>
<jsp:directive.page import="connect.LZ"/>
<jsp:useBean id="a" class="connect.LZ" scope="session"></jsp:useBean>
<% Connection con=null;
Statement stmt=null;
ResultSet rs=null;
String id=request.getParameter("username"); out.print(id);
String password=request.getParameter("password"); out.print(request.getParameter("password"));
String info="0";
try{
con=a.getConnection();
System.out.println("1roorokok");
stmt=con.createStatement();
System.out.println("22roorokok");
rs=stmt.executeQuery("select * from ZCXX where ID='"+id+"'");
System.out.println("33roorokok");
if(rs.next()) {
System.out.println(rs.getString(1));
if(rs.getString("password").equals(password))
{response.sendRedirect("index.jsp"); }
else
{ response.sendRedirect("denglu.jsp?info=2"); }
}
else
{response.sendRedirect("denglu.jsp?info=1");}
}catch(Exception e)
{ System.out.println(e+"erroor");
e.printStackTrace();
}
%>
<html>
<head>
<title>My JSP 'do_denglu.jsp' starting page</title>
</head>
<body>
<div align="center"><br><br><font size="7"><font size="6"><u>登陆成功,欢迎进入网络虚拟教室系统!</u></font>
</font><br>
</div><div align="center"><br><br><br><br><font size="6" style="background-color: rgb(255, 128, 255);"><a href="index.jsp">进入首页</a>
</font><br>
</div></body>
</html>
如果第一次连接的话就没有问题 但是第二次就会出现数据库关闭
错误是[Microsoft][SQLServer 2000 Driver for JDBC]Object has been closed.
请大虾来帮忙啊!
package connect;
import javax.servlet.http.*;
import java.sql.*;
public class LZ implements HttpSessionBindingListener{
Connection con;
Statement sta;
ResultSet rs;
public void valueBound(HttpSessionBindingEvent event){}
public void valueUnbound(HttpSessionBindingEvent event){}
public LZ()
{
BulidConnection();
}
private void BulidConnection()
{
try
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
System.out.println("driver is ok");
con = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName =bysj","sa","xfl039");
System.out.println("conection is ok");
}catch(Exception e)
{
e.printStackTrace();
}
}
public Connection getConnection()
{
if(con==null)
BulidConnection();
return con;
}
}
然后我用它连接数据库作了一个登陆的界面denglu.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<html>
<head>
<base href="<%=basePath%>">
<title>登陆界面</title>
</head>
<body><br>
<center><font size=8>登录界面</font>
<br>
<br>
<%
String info=request.getParameter("info");
if(("1").equals(info))
out.println("<font size=4 color='red'>用户名错误或者不存在,请注册新用户!</font><br><br><br>");
else if(("2").equals(info))
out.println("<font size=4 color='red'>密码不正确,请重新登陆!</font><br><br><br>");
else
out.println("<font size=4 color='red'>请登陆!</font><br><br><br>");
%>
<br>
<br>
<form method='post' action="do_denglu.jsp"><div align="center"><div align="center">
用户名 <input name="username" maxlength="12" "onkeyup="value=value.replace(/[\W]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"><br></div><div align="left"> <br><div align="center">
密码 <input name="password" type="password" maxlength="12" onkeyup="value=value.replace(/[^\d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))">
</div></div></div>
<br>
<div align="center"><input type="submit" name="denglu" value="登陆">
<a href="./zcxx.jsp">新用户注册</a></div>
</form>
</body>
</html>
接着是确认登录的jsp:do_denglu.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ page import="java.sql.*"%>
<jsp:directive.page import="connect.LZ"/>
<jsp:useBean id="a" class="connect.LZ" scope="session"></jsp:useBean>
<% Connection con=null;
Statement stmt=null;
ResultSet rs=null;
String id=request.getParameter("username"); out.print(id);
String password=request.getParameter("password"); out.print(request.getParameter("password"));
String info="0";
try{
con=a.getConnection();
System.out.println("1roorokok");
stmt=con.createStatement();
System.out.println("22roorokok");
rs=stmt.executeQuery("select * from ZCXX where ID='"+id+"'");
System.out.println("33roorokok");
if(rs.next()) {
System.out.println(rs.getString(1));
if(rs.getString("password").equals(password))
{response.sendRedirect("index.jsp"); }
else
{ response.sendRedirect("denglu.jsp?info=2"); }
}
else
{response.sendRedirect("denglu.jsp?info=1");}
}catch(Exception e)
{ System.out.println(e+"erroor");
e.printStackTrace();
}
%>
<html>
<head>
<title>My JSP 'do_denglu.jsp' starting page</title>
</head>
<body>
<div align="center"><br><br><font size="7"><font size="6"><u>登陆成功,欢迎进入网络虚拟教室系统!</u></font>
</font><br>
</div><div align="center"><br><br><br><br><font size="6" style="background-color: rgb(255, 128, 255);"><a href="index.jsp">进入首页</a>
</font><br>
</div></body>
</html>
如果第一次连接的话就没有问题 但是第二次就会出现数据库关闭
错误是[Microsoft][SQLServer 2000 Driver for JDBC]Object has been closed.
请大虾来帮忙啊!