一、数据库结构
1、在本地 localhost,用户名 root ,密码为空,新建数据库 jdbc_study,新建数据表 users 表
users 表结构如下
有七个字段,分别是 id,name,password,birthday,email,create_date,status
二、代码实现
总共有4个类,分别是 DBUtil.java,Users.java,UsersDAO.java,UsersAction.java
采用 MVC 思想,虽然这里没有 View,测试可以在 UsersAction.java
为了方便管理分别建了四个包(package),具体可以看代码头部
DBUtil.java
- package com.liuyanzhao.db;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- /*
- * @author LiuYanzhao
- */
- public class DBUtil {
- private static final String URL = "jdbc:mysql://127.0.0.1:3306/jdbc_study"
- + "?useUnicode=true&characterEncoding=utf8";//防止中文读取乱码
- private static final String USER = "root";
- private static final String PASSWORD = "";
- public static Connection conn = null;
- //静态代码块最先执行,先执行一次
- static {
- try {
- //加载驱动
- Class.forName("com.mysql.jdbc.Driver");
- //获取连接
- conn = DriverManager.getConnection(URL, USER, PASSWORD);
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- //单例模式,使对象唯一性
- public static Connection getConnection() {
- return conn;
- }
- public static void main(String[] args) {
- }
- }
Users.java
- package com.liuyanzhao.model;
- import java.sql.Date;
- /*
- * @author LiuYanzhao
- */
- public class Users {
- private int id;
- private String name;
- private String password;
- private Date birthday;
- private String Email;
- private Date create_date;
- private int status;
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- 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 Date getBirthday() {
- return birthday;
- }
- public void setBirthday(Date birthday) {
- this.birthday = birthday;
- }
- public String getEmail() {
- return Email;
- }
- public void setEmail(String email) {
- Email = email;
- }
- public Date getCreate_date() {
- return create_date;
- }
- public void setCreate_date(Date create_date) {
- this.create_date = create_date;
- }
- public int getStatus() {
- return status;
- }
- public void setStatus(int status) {
- this.status = status;
- }
- }
UsersDAO.java
- package com.liuyanzhao.dao;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.text.ParseException;
- import java.util.ArrayList;
- import java.util.List;
- import com.liuyanzhao.db.DBUtil;
- import com.liuyanzhao.model.Users;
- /*
- * @author LiuYanzhao
- */
- public class UsersDAO {
- //添加用户
- public void addUsers(Users u) throws SQLException, ParseException {
- Connection conn = DBUtil.getConnection();
- String sql = " insert into users "
- + "(name,password,birthday,email,create_date,status)"
- + " values("
- + "?,?,?,?,current_date(),1"
- + ") ";
- PreparedStatement ptmt = conn.prepareStatement(sql);
- ptmt.setString(1, u.getName());
- ptmt.setString(2, u.getPassword());
- ptmt.setDate(3, u.getBirthday());
- ptmt.setString(4, u.getEmail());
- ptmt.executeUpdate();
- }
- //删除用户
- public void deleUser(int id) throws SQLException {
- Connection conn = DBUtil.getConnection();
- String sql = " delete from users where id=?";
- PreparedStatement ptmt = conn.prepareStatement(sql);
- ptmt.setInt(1, id);
- ptmt.executeUpdate();
- }
- //修改用户
- public void updateUser(Users u) throws SQLException {
- Connection conn = DBUtil.getConnection();
- String sql = " update users set "
- + " name=?,password=?,birthday=?,email=?,"
- + " status=?"
- + " where id=?";
- PreparedStatement ptmt = conn.prepareStatement(sql);
- ptmt.setString(1, u.getName());
- ptmt.setString(2, u.getPassword());
- ptmt.setDate(3, u.getBirthday());
- ptmt.setString(4, u.getEmail());
- ptmt.setInt(5, u.getStatus());
- ptmt.setInt(6, u.getId());
- ptmt.executeUpdate();
- }
- //根据id查询单个用户
- public Users get(int id) throws SQLException {
- Connection conn = DBUtil.getConnection();
- String sql = " select * from users where id=?";
- PreparedStatement ptmt = conn.prepareStatement(sql);
- ptmt.setInt(1, id);
- ResultSet rs = ptmt.executeQuery();
- Users u = null;
- while(rs.next()) {
- u = new Users();
- u.setId(rs.getInt("id"));
- u.setName(rs.getString("name"));
- u.setPassword(rs.getString("password"));
- u.setBirthday(rs.getDate("birthday"));
- u.setEmail(rs.getString("email"));
- u.setCreate_date(rs.getDate("create_date"));
- u.setStatus(rs.getInt("status"));
- }
- return u;
- }
- //遍历用户信息
- public List<Users> query() throws SQLException {
- List<Users> result = new ArrayList<Users>();
- Connection conn = DBUtil.getConnection();
- String sql = " select * from users";
- PreparedStatement ptmt = conn.prepareStatement(sql);
- ResultSet rs = ptmt.executeQuery();
- Users u = null;
- while(rs.next()) {
- u = new Users();
- u.setId(rs.getInt("id"));
- u.setName(rs.getString("name"));
- u.setPassword(rs.getString("password"));
- u.setBirthday(rs.getDate("birthday"));
- u.setEmail(rs.getString("email"));
- u.setCreate_date(rs.getDate("create_date"));
- u.setStatus(rs.getInt("status"));
- result.add(u);
- }
- return result;
- }
- }
UsersAction.java
- package com.liuyanzhao.action;
- import java.sql.Date;
- import java.sql.SQLException;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.List;
- import com.liuyanzhao.dao.UsersDAO;
- import com.liuyanzhao.model.Users;
- /*
- * @author LiuYanzhao
- */
- public class UsersAction {
- //添加用户
- public void add(Users u) throws SQLException, ParseException {
- UsersDAO dao = new UsersDAO();
- dao.addUsers(u);
- }
- //删除用户
- public void dele(int id) throws SQLException {
- UsersDAO dao = new UsersDAO();
- dao.deleUser(id);
- }
- //修改用户
- public void edit(Users u) throws SQLException {
- UsersDAO dao = new UsersDAO();
- dao.updateUser(u);
- }
- //根据id查询单条用户
- public Users get(int id) throws SQLException {
- UsersDAO dao = new UsersDAO();
- return dao.get(id);
- }
- //遍历查询所有用户
- public List<Users> query() throws SQLException {
- UsersDAO dao = new UsersDAO();
- return dao.query();
- }
- public static void main(String[] args) throws ParseException, SQLException {
- //添加用户测试
- /*Users u = new Users();
- u.setName("小田");
- u.setPassword("123456");
- u.setBirthday(new Date((new SimpleDateFormat("yyyy-MM-dd").parse("2006-8-10")).getTime()));
- u.setEmail("xiaotian@126.com");
- UsersAction action = new UsersAction();
- action.add(u);*/
- //删除用户测试
- /*UsersAction action2 = new UsersAction();
- action2.dele(5);*/
- //更新用户测试
- /*Users u3 = new Users();
- u3.setId(7);
- u3.setName("小雅");
- u3.setPassword("aaaaaa");
- u3.setBirthday(new Date((new SimpleDateFormat("yyyy-MM-dd").parse("2014-7-21")).getTime()));
- u3.setEmail("xiaoya@126.com");
- u3.setStatus(1);
- UsersAction action3 = new UsersAction();
- action3.edit(u3); */
- //查询用户测试,单条数据
- /*UsersAction action4 = new UsersAction();
- Users u4 = action4.get(7);
- System.out.println(u4.getId());
- System.out.println(u4.getName());
- System.out.println(u4.getPassword());
- System.out.println(u4.getBirthday());
- System.out.println(u4.getEmail());
- System.out.println(u4.getCreate_date());
- System.out.println(u4.getStatus());*/
- //查询所有用户信息,遍历
- UsersAction action5 = new UsersAction();
- List<Users> list = action5.query();
- for(int i=0;i<list.size();i++) {
- System.out.print(list.get(i).getId()+" ");
- System.out.print(list.get(i).getName()+" ");
- System.out.print(list.get(i).getPassword()+" ");
- System.out.print(list.get(i).getBirthday()+" ");
- System.out.print(list.get(i).getEmail()+" ");
- System.out.print(list.get(i).getCreate_date()+" ");
- System.out.print(list.get(i).getStatus()+" ");
- System.out.println();
- }
- }
- }
三、知识拓展
1、JDBC常用API和使用
2、MySQL常用语句
3、execute、executeUpdate、executeQuery三者的区别
4、Java中PreparedStatement和Statement区别
5、java中ArrayList实例讲解
6、Java中如何使某个类的对象唯一性,只能创建一个对象
本文链接:https://liuyanzhao.com/5323.html
转载请务必注明
您可以选择一种方式赞助本站
支付宝扫一扫赞助
微信钱包扫描赞助
赏