1、package com.qy.Bean;public class UserBean /用户idprivate int Id;public int getId() return Id;public void setId(int id) Id = id;public String getUname() return uname;public void setUname(String uname) this.uname = uname;public String getUpwd() return upwd;public void setUpwd(String upwd) this.upwd = up
2、wd;public String getTswt() return tswt;public void setTswt(String tswt) this.tswt = tswt;public String getWtda() return wtda;public void setWtda(String wtda) this.wtda = wtda;/用户姓名private String uname;/用户密 码private String upwd;/提示问题private String tswt;/问题答案private String wtda;package com.qy.DataBase
3、;import java.sql.Connection;import java.sql.DriverManager;public class UserDataBase / 用户名public String uname = “root“;/ 用户密 码public String upwd = “admin“;/ 驱动程序public String DRIVER = “com.mysql.jdbc.Driver“;/ 连接地址public String URL = “jdbc:mysql:/127.0.0.1:3306/qy“;/ 连接public Connection conn;/ 构造方法pu
4、blic UserDataBase() try / 加载驱动程序Class.forName(DRIVER);/ 连接数据库conn = DriverManager.getConnection(URL,uname,upwd); catch (Exception e) / TODO: handle exception/ 连接public Connection conn() / 返回 连接return this.conn;/关闭public void close()try this.conn.close(); catch (Exception e) / TODO: handle exceptionp
5、ackage com.qy.Dao;import com.qy.Bean.UserBean;/java 八步曲/第一步 建立数据库/第二步 建立bean/第三步 连接数据库/第四步 接口/第五步 实现类/第六步 工厂类 /第七步 建立页面/第八步 servlet 处理页面public interface UserDao /用户登 录接口public boolean islogin(String uanme,String upwd)throws Exception;/用户添加public void addUser(UserBean user)throws Exception;package co
6、m.qy.Daoimpl;import java.sql.PreparedStatement;import java.sql.ResultSet;import com.qy.Bean.UserBean;import com.qy.Dao.UserDao;import com.qy.DataBase.UserDataBase;public class UserDaoimpl implements UserDao /定义连接数据库UserDataBase db=null;/操作数据库PreparedStatement pstm;/结果集ResultSet rs;/定义 sql 语句String s
7、ql=null;Override/用户登录public boolean islogin(String uanme, String upwd) throws Exception boolean bol=false;/连接数据库db=new UserDataBase();sql=“select uanme,upwd from qyuser where uanme=? and upwd=?“;pstm=db.conn().prepareStatement(sql);/System.err.println(sql);pstm.setString(1, uanme);pstm.setString(2,
8、upwd);rs=pstm.executeQuery();if(rs.next()bol=true;db.close();pstm.close();rs.close();return bol;Override/ 添加用户public void addUser(UserBean user) throws Exception / TODO Auto-generated method stubdb=new UserDataBase();/sql 语句sql=“insert into qyuser(uanme,upwd,tswt,wtda)values(?,?,?,?)“;/pstmpstm=db.c
9、onn.prepareStatement(sql);pstm.setString(1, user.getUname();pstm.setString(2, user.getUpwd();pstm.setString(3, user.getTswt();pstm.setString(4, user.getWtda();/添加pstm.executeUpdate();/关闭流pstm.close();db.close();package com.qy.Factory;import com.qy.Dao.UserDao;import com.qy.Daoimpl.UserDaoimpl;public
10、 class UserDaoimplFactory /把接口和实现类整合在一起public static UserDao userFactory()return new UserDaoimpl();package com.qy.Factory;import com.qy.Dao.UserDao;import com.qy.Daoimpl.UserDaoimpl;public class UserDaoimplFactory /把接口和实现类整合在一起public static UserDao userFactory()return new UserDaoimpl();“My JSP addus
11、er.jsp starting page添加用 户用户姓名:用户密码:提示问题:问题答案:/数据库字段# MySQL-Front 3.2 (Build 7.19)/*!40101 SET OLD_CHARACTER_SET_CLIENT=CHARACTER_SET_CLIENT */;/*!40101 SET OLD_CHARACTER_SET_RESULTS=CHARACTER_SET_RESULTS */;/*!40101 SET OLD_COLLATION_CONNECTION=COLLATION_CONNECTION */;/*!40101 SET NAMES gbk */;# Hos
12、t: 127.0.0.1 Database: qy# -# Server version 5.0.18-nt# Table structure for table qyuser#DROP TABLE IF EXISTS qyuser;CREATE TABLE qyuser (Id int(11) NOT NULL auto_increment,uanme varchar(50) default NULL,upwd varchar(50) default NULL,tswt varchar(50) default NULL,wtda varchar(50) default NULL,PRIMAR
13、Y KEY (Id) ENGINE=InnoDB DEFAULT CHARSET=gbk;# Dumping data for table qyuser#INSERT INTO qyuser VALUES (1,admin,admin,admin,admin);INSERT INTO qyuser VALUES (2,suqin,suqin,suqin,suqin);INSERT INTO qyuser VALUES (3,123,123,苏秦,苏秦);INSERT INTO qyuser VALUES (4,321,321,苏秦,苏秦);INSERT INTO qyuser VALUES (7,111,111,111,111);/*!40101 SET CHARACTER_SET_CLIENT=OLD_CHARACTER_SET_CLIENT */;/*!40101 SET CHARACTER_SET_RESULTS=OLD_CHARACTER_SET_RESULTS */;/*!40101 SET COLLATION_CONNECTION=OLD_COLLATION_CONNECTION */;