jsp 用javabean连接数据库mysql问题

Bonjourss 2013-04-02 09:23:51
出现严重: Servlet.service() for servlet jsp threw exception
org.apache.jasper.JasperException: /process.jsp(4,0) The value for the useBean class attribute dbbean.DBBean is invalid.

dbbean:
package dbbean;
import java.sql.*;
//import java.util.*;
public class DBBean {


private Connection conn=null;
private Statement stmt=null;
private ResultSet rs=null;
String connStr = "jdbc:mysql://localhost:3306/student";


public DBBean(){
// if(conn!=null) return;
try{
Class.forName("com.mysql.jdbc.Dirver");
conn=DriverManager.getConnection( connStr,"root","123");//建立和数据库之间的连接
}catch(ClassNotFoundException e){
//e.printStackTrace();
System.out.println("驱动的类名找不到异常。");
}catch(SQLException e){
//e.printStackTrace();
System.out.println("SQL语句异常。");
}
}

public ResultSet executeQuery(String sql){
//ResultSet rs=null;
try{
//获取驱动程序对象和指定树之间的连接对象
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/student","root","123");
stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
}catch(SQLException e){
System.out.println();
}
return rs;

}


public int executeUpdate(String sql){
int result=0;
try{
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/student","root","123");
stmt=conn.createStatement();
result=stmt.executeUpdate(sql);
}catch(SQLException e){
System.out.println();
}
return result;
}


public void close(){
try{
if(rs!=null){
rs.close();
rs=null;
}
if(stmt!=null){
stmt.close();
stmt=null;
}
if(conn!=null){
conn.close();
conn=null;
}

}catch(SQLException e){
e.printStackTrace();
}
}

}


跳转后的页面jsp页面:
<%--设置字符集,保证能够正确显示中文--%>
<%@ page contentType="text/html" pageEncoding="utf-8" import="java.sql.*"%>
<%--声明和创建数据库访问Bean的对象conn,生存时间改为session --%>
<jsp:useBean id="conn" class="dbbean.DBBean" scope="session" />
<%--设置conn对象的connStr属性值为“jbdc:odbc:user”,及所连接的数据库的url,这里的user为数据库对应的数据源名 --%>
<jsp:setProperty name="conn" property="connStr" value="com.mysql.jdbc.Dirver"/>
<%

//将session的生存时间设为无限大,避免因session过期引起的问题
session.setMaxInactiveInterval(-1);
//获取用户名
String username=request.getParameter("username");
//获取用户名进行中文转换,防止中文乱码问题
username=new String(username.getBytes("gb2312"));
//获取口令
String password=request.getParameter("password");
//对口令进行编码转换
password=new String(password.getBytes("gb2312"));
//查询数据表user中是否存在匹配记录
String sql="select * from user where name='"+username+"'and password='"+password+"' ";
ResultSet rs=null;
rs=conn.executeQuery(sql);
//存在匹配记录
if(rs.next())
{
//将登陆的id写入到session中
session.setAttribute("loginId",rs.getString("id"));
//将登陆用户写入到session中
session.setAttribute("username",rs.getString("name"));
//关闭数据库访问Bean对象
conn.close();
//重定向到系统内部功能页面
response.sendRedirect("home.html");
}
else
{

//重定向到系统登录页面
response.sendRedirect("login.html");

}

%>

各位大侠求帮忙。。。
...全文
92 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

24,923

社区成员

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

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