用javabean向数据库添加数据,请问为什么DoInsert.jsp一直报错

package com.javabean.user;
//用于封装用户信息
public class User
{
private int id;
private String username;
private String password;
private String type;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}


package com.javabean.user;
//用户连接数据库------获得特定数据库的连接对象,使以后可以调查此方法来访问数据库
import java.sql.*;
public class Conn
{
public static final String DBDRIVER="com.mysql.jdbc.Driver"; //包加类名
public static final String DBURL="jdbc:mysql://localhost:3306/test";
public static final String DBUSER="root";
public static final String DBPASS="123456";

public Connection getConn()
{
   Connection conn=null;
   try
   {
       Class.forName(DBDRIVER);
       conn=DriverManager.getConnection(DBURL,DBUSER,DBPASS);
   }catch(Exception e)
   {
    e.printStackTrace();
   }
   return conn;
}

}

package com.javabean.user;
//对数据库的增删改查操作
import com.javabean.user.Conn;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

import com.javabean.user.User;
public class Userdao
{
//添加数据
public int InsertUser(User usera)
{
int rs=0;
try
{
//驱动连接了数据库
Conn conn=new Conn();
Connection dbc=conn.getConn();
//插入语句
String sql_insert="insert into user(username,password,type) values(?,?,?)";
//将SQl语句预编译保存在pst中
PreparedStatement pst=dbc.prepareStatement(sql_insert);
//为sql语句中的属性赋值
pst.setString(1,usera.getUsername());
pst.setString(2,usera.getPassword());
pst.setString(3,usera.getType());
//执行sql语句,将结果放在rs中
rs=pst.executeUpdate();

  }catch(Exception e)
  {
      e.printStackTrace();
  }
  return rs;

}

public List SelectUser()
{
ResultSet rs=null;
List list=new ArrayList();
try
{
Conn conn=new Conn();
Connection dbc=conn.getConn();

      String sql_select="select * from user";
      PreparedStatement pst=dbc.prepareStatement(sql_select);
      rs=pst.executeQuery();

      while(rs.next())
      {
          User user=new User();
          user.setId(rs.getInt(1));
          user.setUsername(rs.getString(2));
          user.setPassword(rs.getString(3));
          user.setType(rs.getString(4));
          list.add(user);
      }
  }catch(Exception e)
  {
      e.printStackTrace();
  }
  return list;

}

}

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



DoInsert


<%--对Inser.jsp页面中表单的提交请求进行处理,该页面将获取表单提交的所有信息,然后将信息添加到数据库--%>

<%request.setCharacterEncoding("utf-8"); %>



/jsp:useBean

<%
Userdao userdaoa=new Userdao();
userdaoa.InsertUser(user);
%>


2个回答

代码是全的吗? 看JSP中:userdaoa.InsertUser(user); 这里边的user哪来的, 没有进行初始化吧?!

user对象在哪初始化的呢

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