Java之JDBC连接数据库实现增删改查(2018 使用Dao层实现)

版权声明:《==study hard and make progress every day==》 https://blog.csdn.net/qq_38225558/article/details/82842426

层次结构如图:

用户账号实体类 User.java

/**
 * 用户账号实体类
 * @author 郑清
 */
public class User {
	
	private Integer id;
	private String username;
	private String password;
	
	public User() {}
	
	public Integer getId() {
		return id;
	}	
	public void setId(Integer id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}	
	public void setUsername(String username) {
		this.username = username;
	}	
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + ", password=" + password + "]";
	}
	
}

UserDao接口类 IUserDao.java

public interface IUserDao {
	void add(User user);
	void delete(Integer id);
	void update(User user);
	User queryOne(Integer id);
	List<User> queryAll();
}

实现接口类  UserDaoImpl.java

public class UserDaoImpl implements IUserDao {

	@Override
	public void add(User user) {
		// TODO Auto-generated method stub
		Connection connection = null;
		Statement statement = null;
		try {
			//1.加载驱动
			Class.forName("com.mysql.jdbc.Driver");
			//2.获取连接:与数据库建立连接
			connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/zhengqing_test", "root", "root");
			//3.获得sql的预编译对象
			statement = connection.createStatement();
			//4.执行sql语句    ex:创建表t_user
//			String sql = "insert into t_user(id,username,password) values(2,'张三','123'),(10,'李四','123456');";
			String sql = "insert into t_user values("+user.getId()+",'"+user.getUsername()+"','"+user.getPassword()+"');";
			statement.executeUpdate(sql);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			//5.释放资源      (注意:关闭资源顺序 先打开后关闭)
			try {
				if(statement!=null) {
					statement.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}finally{
				try {
					if(connection!=null) {
						connection.close();
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}	
	}

	@Override
	public void delete(Integer id) {
		// TODO Auto-generated method stub
		Connection connection = null;
		Statement statement = null;
		try {
			//1.加载驱动
			Class.forName("com.mysql.jdbc.Driver");
			//2.获取连接:与数据库建立连接
			connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/zhengqing_test", "root", "root");
			//3.获得sql的预编译对象
			statement = connection.createStatement();
			//4.执行sql语句    ex:删除表t_user中的数据
//			String sql = "delete from t_user where id = 2;";
			String sql = "delete from t_user where id = "+id+";";
			statement.executeUpdate(sql);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			//5.释放资源      (注意:关闭资源顺序 先打开后关闭)
			try {
				if(statement!=null) {
					statement.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}finally{
				try {
					if(connection!=null) {
						connection.close();
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}

	@Override
	public void update(User user) {
		// TODO Auto-generated method stub
		Connection connection = null;
		Statement statement = null;
		try {
			//1.加载驱动
			Class.forName("com.mysql.jdbc.Driver");
			//2.获取连接:与数据库建立连接
			connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/zhengqing_test", "root", "root");
			//3.获得sql的预编译对象
			statement = connection.createStatement();
			//4.执行sql语句    ex:修改表t_user中数据
//			String sql = "update t_user set username='修改名字',password='111111' where id=3;";
			String sql = "update t_user set username ='"+user.getUsername()+"',password='"+user.getPassword()+"' where id ="+user.getId()+";";
			statement.executeUpdate(sql);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			//5.释放资源      (注意:关闭资源顺序 先打开后关闭)
			try {
				if(statement!=null) {
					statement.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}finally{
				try {
					if(connection!=null) {
						connection.close();
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}

	@Override
	public User queryOne(Integer id) {
		// TODO Auto-generated method stub
		Connection connection = null;
		Statement statement = null;
		ResultSet rs = null;
		User user = null;
		try {
			//1.加载驱动
			Class.forName("com.mysql.jdbc.Driver");
			//2.获取连接:与数据库建立连接
			connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/zhengqing_test", "root", "root");
			//3.获得sql的预编译对象
			statement = connection.createStatement();
			//4.执行sql语句    ex:查找表t_user中数据
			String sql = "select * from t_user where id = "+id+";";
			rs = statement.executeQuery(sql);
			while(rs.next()){
				user = new User();
				user.setId(rs.getInt("id"));
				user.setUsername(rs.getString("username"));
				user.setPassword(rs.getString("password"));
//				System.out.print(rs.getInt("id")+"-->");
//				System.out.print(rs.getString("username")+"-->");
//				System.out.println(rs.getString("password"));
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			//5.释放资源      (注意:关闭资源顺序 先打开后关闭)
			try {
				if(rs!=null)rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}finally{
				try {
					if(statement!=null)statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}finally{
					try {
						if(connection!=null)connection.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
			}
		}
		return user;
	}

	@Override
	public List<User> queryAll() {
		// TODO Auto-generated method stub
		Connection connection = null;
		Statement statement = null;
		ResultSet rs = null;
		List<User> userList = new ArrayList<>();
		try {
			//1.加载驱动
			Class.forName("com.mysql.jdbc.Driver");
			//2.获取连接:与数据库建立连接
			connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/zhengqing_test", "root", "root");
			//3.获得sql的预编译对象
			statement = connection.createStatement();
			//4.执行sql语句    ex:查找表t_user中数据
			String sql = "select * from t_user;";
			rs = statement.executeQuery(sql);
			while(rs.next()){
				User user = new User();
				user.setId(rs.getInt("id"));
				user.setUsername(rs.getString("username"));
				user.setPassword(rs.getString("password"));
				userList.add(user);
//				System.out.print(rs.getInt("id")+"-->");
//				System.out.print(rs.getString("username")+"-->");
//				System.out.println(rs.getString("password"));
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			//5.释放资源      (注意:关闭资源顺序 先打开后关闭)
			try {
				if(rs!=null)rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}finally{
				try {
					if(statement!=null)statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}finally{
					try {
						if(connection!=null)connection.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
			}
		}
		return userList;
	}

}

使用Junit4测试 数据库的增删改查 UserDaoTest.java

/**
 * 使用Junit4测试 数据库的增删改查
 * @author 郑清
 */
public class UserDaoTest {
	IUserDao userDao = new UserDaoImpl();
	
	@Test
	public void testAdd() {
		User user = new User();
		user.setUsername("hello3");
		user.setPassword("123456");
		userDao.add(user);
	}
	
	@Test
	public void testUpdate(){
		User user = new User();
		user.setId(3);
		user.setUsername("翠花");
		user.setPassword("666666");
		userDao.update(user);
	}
	
	@Test
	public void testDelete(){
		userDao.delete(15);
	}
	
	@Test
	public void testQueryOne(){
		User user = userDao.queryOne(3);
		System.out.println(user);
	}
	
	@Test
	public void testQueryAll(){
		List<User> users = userDao.queryAll();
		for (User user : users) {
			System.out.println(user);
		}
	}
	
}

点击下载项目资源  密码:e3pn

猜你喜欢

转载自blog.csdn.net/qq_38225558/article/details/82842426