java web javabean的使用 4C

写了一个连接数据库的bean,然后使用Javabean时报错The value for the useBean class attribute bean.DBBean is invalid. 拿自己写的代码原封不动的放到别的电脑上之后运行无错误,可以向数据库里添加数据。
代码如下:
bean:
package bean;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class DBBean {
private String driverStr = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private String conStr = "jdbc:sqlserver://localhost:1433; DatabaseName=G_Forum";
private String dbusername = "sa";
private String dbpassword = "153532";
private Connection conn = null;
private Statement stmt = null;

public DBBean(){
    try {
        Class.forName(driverStr);
        conn = DriverManager.getConnection(conStr, dbusername, dbpassword);
        stmt = conn.createStatement();
    } catch (Exception ex) {
        System.out.println("无法同数据库建立连!");
    }
}

public int executeUpdate(String s) {
    int result = 0;
    try {
        result = stmt.executeUpdate(s);
    } catch (Exception ex) {
        System.out.println("执行更新错误++++++!");
    }
    return result;
}

public ResultSet executeQuery(String s) {
    ResultSet rs = null;
    try {
        rs = stmt.executeQuery(s);
    } catch (Exception ex) {
        System.out.println("执行查询错误!");
    }
    return rs;
}

public void close() {
    try {
        stmt.close();
        conn.close();
    } catch (Exception e) {
    }
}

}

jsp:
<%@ page import="java.sql.*" language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">



用户注册



<%
request.setCharacterEncoding("UTF-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
String sex = request.getParameter("sex");
String age = request.getParameter("age");
String phone = request.getParameter("phone");
String email = request.getParameter("email");
String s ="Select * From user";
ResultSet rs =db.executeQuery(s);
boolean exist = true;
while(rs.next())
{
if(username.equals(rs.getString(1)))
{
exist = false;
out.println(" alert(&#39;用户名已存在,点击确定回到注册页面。&#39;);");
response.setHeader("refresh", "1;url=zc.jsp");
}
}
if(exist)
{
String InsertSQL = "Insert Into user Values('" + username + "','" + password + "','" + sex + "','" + age + "','" +phone + "','" + email + "')";
int i = db.executeUpdate(InsertSQL);
if (i == 1)
{
out.println(" alert(&#39;注册成功,点击确定回到登陆页面。&#39;);");
response.setHeader("refresh", "1;url=Login.html");
}
}
%>

5个回答

抱歉 少贴一行代码

这个就是导入啊,为什么还是报错usebean没有被服务器发现运行(useBean class attribute bean.DBBean is invalid.)

你jsp页面没贴对,要有登录ui

什么意思?我有登录ui啊,我的问题报错是useBean class attribute bean.DBBean is invalid. 百度了都说是usebean没有被服务器发现运行的问题,但没有好的解决方案

菜鸟 跪求大神帮助啊!!!!

JSP里面用JAVA类是要导入的 JSP本质上是一个servlet类

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐