我是想在jsp里面调用Bean来增加数据到数据库的,实际代码如下,执行后说插入不成功,各位看问题出在哪里:
Bean.java
--------------------------------------------------------
package bean;
import java.sql.*;public class Bean extends Dcl{
//以下是用户表操作部分
public boolean adduser(){
boolean bu_adduser=false;
try{
String usersql="insert into usename(name,password,dept,pop) values("+name+","+password+","+dept+","+pop+")";
if(super.insert(usersql))bu_adduser=true;
}catch(Exception e){
System.out.println("增加用户失败");
}finally{
return bu_adduser;
}
}public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public String getPassword(){
return password;
}
public void setPassword(String password){
this.password=password;
}
public String getDept(){
return dept;
}
public void setDept(String dept){
this.dept=dept;
}
public String getPop(){
return pop;
}
public void setPop(String pop){
this.pop=pop;
}}Dcl.java
------------------------------------------------------------------------------
//package bean;import java.sql.*;public class Dcl{
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=myedit";//数据库地址
String sa="sa";//数据库用户
String pw="hello";//数据库密码
String driver="com.microsoft.jdbc.sqlserver.SQLServerDriver";//数据库驱动
//String newsid;//文章ID
String username="";//作者ID
String title="";//文章标题
String content="";//文章内容
String datenews="";//文章日期
//String usernameid;//用户表ID号
String name="";//用户名
String password="";//用户密码
String dept="";//用户类型
String pop="";//权限
String sql="";
ResultSet rs;public Dcl(){
try{
Class.forName(driver).newInstance();
}catch(Exception e){
System.out.println("驱动加载失败");
}}public boolean insert(String sql){
boolean boup=false;
rs=null;
try{
Connection conn=DriverManager.getConnection(url,sa,pw);
Statement stmt=conn.createStatement();
int insertcount=stmt.executeUpdate(sql);
if(insertcount>0)boup=true;
}catch(SQLException e){
System.out.println("增加失败");
}finally{
return boup;
}
}}
adduser.jsp
------------------------------------------------------------
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %> <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>系统</title>
</head><body bgcolor="#FFFFFF" text="#996699" link="#FF9900" vlink="#663366" alink="#FF00FF">
<div align="center"> 
  <form action="adduser1.jsp" method="POST" name="form1">
    <table width="300" border="1" cellspacing="1" cellpadding="0">
      <tr align="center"> 
        <td height="20" colspan="2">增加用户</td>
      </tr>
      <tr> 
        <td width="82" height="20" align="right">用&nbsp;户:</td>
        <td width="209" height="20"> <input name="name" type="text" id="name1"></td>
      </tr>
      <tr> 
        <td height="20" align="right">密&nbsp;码:</td>
        <td height="20"> <input name="password" type="password" id="password1"></td>
      </tr>
      <tr>
        <td height="20" align="right">部&nbsp;门:</td>
        <td height="20"><select name="dept">
            <option value="dept1" selected>金钱部</option>
            <option value="dept2">银钱部</option>
            <option value="dept3">铜钱部</option>
            <option value="dept4">铁钱部</option>
          </select></td>
      </tr>
      <tr>
        <td height="20" align="right">权&nbsp;限:</td>
        <td height="20"><p>
            <label>
            <input type="radio" name="pop" value="1">
            管理员</label>
            <br>
            <label>
            <input type="radio" name="pop" value="2">
            普通</label>
            <br>
          </p></td>
      </tr>
      <tr align="center"> 
        <td height="20" colspan="2"> <input type="submit" name="Submit" value="提交"> 
          &nbsp; <input type="reset" name="Submit2" value="重置"> </td>
      </tr>
    </table>
  </form>
</div>
</body>
</html>adduser1.jsp
-------------------------------------------------------
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %><jsp:useBean id="bean1" scope="application" class="bean.Bean"/>
<jsp:setProperty name="bean1" property="*" /><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>增加用户处理</title>
</head><body>
<% 
if (bean1.adduser()){
out.println("增加成功!");
}else{
out.println("增加失败");
}
 %>
<jsp:getProperty name="bean1" property="name" />
<jsp:getProperty name="bean1" property="password" />
<jsp:getProperty name="bean1" property="dept" />
<jsp:getProperty name="bean1" property="pop" />
</body>
</html>