时间:2021-07-01 10:21:17 帮助过:5人阅读
User.java
1 package com.mhb; 2 3 public class User { 4 //用户ID 5 private int userid; 6 //用户姓名 7 private String username; 8 //用户密码 9 private String password; 10 11 public int getUserid() { 12 return userid; 13 } 14 public void setUserid(int userid) { 15 this.userid = userid; 16 } 17 public String getUsername() { 18 return username; 19 } 20 public void setUsername(String username) { 21 this.username = username; 22 } 23 public String getPassword() { 24 return password; 25 } 26 public void setPassword(String password) { 27 this.password = password; 28 } 29 }
UserDAO.java
1 package com.mhb; 2 import java.util.*; 3 4 //定义数据库操作方法 5 public interface UserDAO { 6 //增加操作 7 public void insert (User user) throws Exception; 8 //修改操作 9 public void update (User user) throws Exception; 10 //删除操作 11 public void delete (int userid) throws Exception; 12 //按ID查询 13 public User queryById(int userid) throws Exception; 14 //查询全部 15 public List queryAll() throws Exception; 16 17 }
UserDAOImpl.java
1 package com.mhb; 2 3 import java.sql.PreparedStatement; 4 import java.util.List; 5 6 public class UserDAOImpl implements UserDAO { 7 8 //添加操作 9 public void insert(User user) throws Exception { 10 String sql = "INSERT INTO user(username,password) VALUES (?,?)"; 11 PreparedStatement pstmt = null; 12 DataBaseConnection dbc = null; 13 //下面是针对数据库的具体操作 14 try{ 15 dbc= new DataBaseConnection(); 16 pstmt = dbc.getConnection().prepareStatement(sql); 17 pstmt.setString(1, user.getUsername()); 18 pstmt.setString(2, user.getPassword()); 19 //进行数据库更新 20 pstmt.executeUpdate(); 21 pstmt.close(); 22 }catch(Exception e){ 23 throw new Exception ("操作出现异常"); 24 }finally{ 25 //关闭数据库 26 dbc.close(); 27 } 28 } 29 30 public void delete(int userid) throws Exception { 31 32 } 33 34 35 36 public List queryAll() throws Exception { 37 return null; 38 }
DAOFactory.java
1 package com.mhb; 2 3 public class DAOFactory { 4 public static UserDAO getUserDAOInstance(){ 5 return new UserDAOImpl(); 6 } 7 }
MySql数据库中user表语句
1 CREATE TABLE `user` ( 2 `userid` int(11) NOT NULL AUTO_INCREMENT, 3 `username` varchar(20) DEFAULT NULL, 4 `password` varchar(20) DEFAULT NULL, 5 PRIMARY KEY (`userid`) 6 ) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8
至此,DAO的5个部分就以全部结束,接下来通过DAO工厂获得DAO实现类实例。
通过DAO实现类中的方法就可以完成添加记录的数据库操作了。
添加记录AddUserDemo.jsp
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <%@ page import="com.mhb.*" %> 3 4 <html> 5 <head><title>添加用户</title></head> 6 7 <body> 8 <form action="" method="post"> 9 <table> 10 <tr> 11 <td colspan="2">添加用户</td> 12 </tr> 13 <tr> 14 <td>用户姓名:</td> 15 <td><input type="text" name="username"></td> 16 </tr> 17 <tr> 18 <td>用户密码</td> 19 <td><input type="password" name="password"></td> 20 </tr> 21 <tr> 22 <td colspan="2"> 23 <input type="submit" value="添加"> 24 <input type="reset" value="重置"> 25 </td> 26 </tr> 27 </table> 28 </form> 29 30 <% 31 //获取表单中输入的用户名以及密码 32 String userName = (String)request.getParameter("username"); 33 String password = (String)request.getParameter("password"); 34 //通过DAO工厂获得DAO实现类的实例 35 UserDAO userDAO = DAOFactory.getUserDAOInstance(); 36 //设置需要添加的用户 37 User user = new User(); 38 user.setUsername(userName); 39 user.setPassword(password); 40 //如果用户名密码不为空则添加入数据库 41 if(userName != null && userName != ""){ 42 if(password != null && password != ""){ 43 userDAO.insert(user); 44 } 45 }else{ 46 out.print("用户名或密码不能为空!"); 47 } 48 %> 49 </body> 50 </html>
浏览器显示
username:admin,password:123456
数据库显示
DAO设计模式 -- 使用数据库连接类连接MySql数据库并实现添加用户
标签: