我用JB2005,TOMCAT,MSSQL创建简单的修改数据库页面,通过creat.jsp传递表单值到switch.jsp,然后通过user.java自动获取,代码如下:
creat.jsp
<%@ page contentType="text/html; charset=GBK" %>
<meta http-equiv="refresh" content=120>
<html>
<head>
<title>
creat
</title>
</head>
<form name="form1" method="post" action="switch.jsp">
<input type="text" name="ID" value="ID">
<input type="text" name="PS" value="PASSWORD">
<input type="submit" name="Submit" value="提交">
</form>
<body bgcolor="#ffffff">
</body>
</html>
switch.jsp
<%@ page contentType="text/html; charset=GBK" %>
<%@page import="java.sql.*"%>
<jsp:useBean id="insert" class="insert.user" scope="page"  />
<jsp:setProperty name="insert" property="*" />
<%
String driver= "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url ="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=master";
String user="sa";
String passw="sa";
String userId=Bean.getUserId();
String password=Bean.getPassword();
String sql="insert into data(ID,PS) values('"+userId+"','"+password+"')";
Statement stmt;
Connection con;
            try{
                Class.forName(driver);
                con = DriverManager.getConnection(url,user,passw);
                stmt=con.createStatement();
                stmt.executeUpdate(sql);
                stmt.close();
                con.close();
            }
            catch(ClassNotFoundException e){
                System.out.println(e.getMessage());
            }
            catch(SQLException e){
                System.out.println(e.getMessage());
            }
%>
user.java
package insert;
public class user {
    public user(){}
private String ID;
private String PS;
public String getPassword() {
return PS;
}
public String getUserId() {
return ID;
}
public void setPassword(String PS) {
this.PS = PS;
}
public void setUserId(String ID) {
this.ID = ID;
}
}
不知道为什么,调试时,IE总是出现这样的错误:
org.apache.jasper.JasperException: /insert/WebModule1/switch.jsp(3,0) The value for the useBean class attribute insert.user is invalid.
难道我的user.java错了吗?希望各位帮帮忙!

解决方案 »

  1.   

    你不先在user中set值,怎么就能取到??
      

  2.   

    userBean中class的值错误,查查看有没有写错
      

  3.   

    USER.JAVA也没有显示错误啊,我是用JB做的,我也想是那里错,但就不知错哪里
      

  4.   

    我认为你有两处错误:第一 你在switch.jsp中去值的时候不应该用insert.getUserId();吗?同理密码也是一样的。
    第二 你的jsp名字不要用switch 你很喜欢用一些关键字或者特殊作为名字很不好。我认为如果你的数据库连接没问题的话就可以了!!!
      

  5.   

    谢谢你第二点的指出,很中肯,第一点的错误已经改了,不过还是说
    org.apache.jasper.JasperException: /insert/WebModule1/switch.jsp(3,0) The value for the useBean class attribute insert.user is invalid.
      

  6.   

    总是说我的insert.user错误,真不知错在哪里
      

  7.   

    user.java
    package insert;
    public class user {
        public user(){}
    private String id;
    private String ps;
    public String getPs() {
    return PS;
    }
    public String getId() {
    return id;
    }
    public void setPs(String PS) {
    ps = PS;
    }
    public void setId(String ID) {
    id = ID;
    }
    }
    看看bean的定义规则
      

  8.   

    String sql="insert into data(ID,PS) values('"+userId+"','"+password+"')";
    -----------------------------
    问题我想是这里了
    如果你的userID是数字型的话就不要双引号
    而是单引号.检查你的数据类型试试
      

  9.   

    insert 如果是int型的加''是可以插入的!但要保证都是数字 没有空格之类的