日期:2014-05-18  浏览次数:20703 次

关于javabean操作数据库问题
我是想在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> 系统 </t