JDBC增删改查
操作步骤
1、加载驱动
2、获取连接
3、执行相应的操作(增、删、改、查)
4、释放资源
JDBCTest
package com.lyh;
import com.lyh.dao.UserDaoImpl;
import com.lyh.pojo.User;
import java.sql.Date;
import java.util.List;
/**
* @author martin
* @date 2020/9/27
**/
public class JDBCTest {
public static void main(String[] args) {
UserDaoImpl dao = new UserDaoImpl();
// 查询所有用户
List<User> userList = dao.selectAllUser();
for (User user : userList) {
System.out.println(user);
}
System.out.println("==========");
// 根据名字查询用户
System.out.println(dao.selectUserByName("李四"));
System.out.println("==========");
// 插入用户
User user = new User(2, "李四", "111111", "li@163.com", new Date(System.currentTimeMillis()));
System.out.println(dao.insertUser(user));
System.out.println("==========");
// 删除用户
System.out.println(dao.deleteUser(4));
System.out.println("==========");
// 修改用户
System.out.println(dao.updateUser(user));
System.out.println("==========");
List<User> users = dao.selectAllUser();
for (User u : users) {
System.out.println(u);
}
}
}
User
package com.lyh.pojo;
import java.sql.Date;
/**
* @author martin
* @date 2020/9/27
**/
public class User {
private int id;
private String name;
private String password;
private String email;
private Date birthday;
public User() {
}
public User(int id, String name, String password, String email, Date birthday) {
this.id = id;
this.name = name;
this.password = password;
this.email = email;
this.birthday = birthday;
}
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 String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
", email='" + email + '\'' +
", birthday=" + birthday +
'}';
}
}
BaseDao
package com.lyh.dao;
import java.sql.*;
/**
* @author martin
* @date 2020/9/27
**/
public class BaseDao {
private final static String driver = "com.mysql.cj.jdbc.Driver";
private final static String url = "jdbc:mysql://localhost:3306/jdbc_study?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC";
private final static String username = "root";
private final static String password = "root";
public static Connection getConnection() {
Connection connection = null;
try {
// 加载驱动
Class.forName(driver);
// 获取连接
connection = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
return connection;
}
public static boolean closeResource(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet) {
boolean flag = true;
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
flag = false;
}
}
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
flag = false;
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
flag = false;
}
}
return flag;
}
}
UserDao
package com.lyh.dao;
import com.lyh.pojo.User;
import java.util.List;
/**
* @author martin
* @date 2020/9/27
**/
public interface UserDao {
/**
* 增加用户
* @param user
* @return
*/
int insertUser(User user);
/**
* 根据id删除用户
* @param id
* @return
*/
int deleteUser(int id);
/**
* 修改用户
* @param user
* @return
*/
int updateUser(User user);
/**
* 根据名字查询用户
* @param name
* @return
*/
User selectUserByName(String name);
/**
* 查找所有用户
* @return
*/
List<User> selectAllUser();
}
UserDaoImpl
package com.lyh.dao;
import com.lyh.pojo.User;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
* @author martin
* @date 2020/9/27
**/
public class UserDaoImpl implements UserDao{
@Override
public int insertUser(User user) {
int n = 0;
String sql = "insert into users values (default, ?, ?, ?, ?)";
Connection conn = null;
PreparedStatement preparedStatement = null;
try {
// 获取连接
conn = BaseDao.getConnection();
preparedStatement = conn.prepareStatement(sql);
preparedStatement.setString(1, user.getName());
preparedStatement.setString(2, user.getPassword());
preparedStatement.setString(3, user.getEmail());
preparedStatement.setDate(4, user.getBirthday());
n = preparedStatement.executeUpdate();
// 释放资源
BaseDao.closeResource(conn, preparedStatement, null);
} catch (SQLException e) {
e.printStackTrace();
}
return n;
}
@Override
public int deleteUser(int id) {
int n = 0;
String sql = "delete from users where id = ?";
PreparedStatement preparedStatement = null;
Connection conn = null;
try {
// 获取连接
conn = BaseDao.getConnection();
preparedStatement = conn.prepareStatement(sql);
preparedStatement.setInt(1, id);
n = preparedStatement.executeUpdate();
// 释放资源
BaseDao.closeResource(conn, preparedStatement, null);
} catch (SQLException e) {
e.printStackTrace();
}
return n;
}
@Override
public int updateUser(User user) {
int n = 0;
String sql = "update users set name = ?, password = ?, email = ?, birthday = ? where id = ?";
Connection conn = null;
PreparedStatement preparedStatement = null;
try {
// 获取连接
conn = BaseDao.getConnection();
preparedStatement = conn.prepareStatement(sql);
preparedStatement.setString(1, user.getName());
preparedStatement.setString(2, user.getPassword());
preparedStatement.setString(3, user.getEmail());
preparedStatement.setDate(4, user.getBirthday());
preparedStatement.setInt(5, user.getId());
n = preparedStatement.executeUpdate();
// 释放资源
BaseDao.closeResource(conn, preparedStatement, null);
} catch (SQLException e) {
e.printStackTrace();
}
return n;
}
@Override
public User selectUserByName(String name) {
User user = null;
String sql = "select * from users where name = ?";
Connection conn = null;
PreparedStatement preparedStatement = null;
try {
conn = BaseDao.getConnection();
preparedStatement = conn.prepareStatement(sql);
preparedStatement.setString(1, name);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
user = new User();
user.setId(resultSet.getInt("id"));
user.setName(resultSet.getString("name"));
user.setPassword(resultSet.getString("password"));
user.setEmail(resultSet.getString("email"));
user.setBirthday(resultSet.getDate("birthday"));
}
// 释放资源
BaseDao.closeResource(conn, preparedStatement, resultSet);
} catch (SQLException e) {
e.printStackTrace();
}
return user;
}
@Override
public List<User> selectAllUser() {
List<User> userList = new ArrayList<>();
Connection conn = null;
PreparedStatement preparedStatement = null;
String sql = "select * from users";
try {
conn = BaseDao.getConnection();
preparedStatement = conn.prepareStatement(sql);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
User user = new User();
user.setId(resultSet.getInt("id"));
user.setName(resultSet.getString("name"));
user.setPassword(resultSet.getString("password"));
user.setEmail(resultSet.getString("email"));
user.setBirthday(resultSet.getDate("birthday"));
userList.add(user);
}
// 释放资源
BaseDao.closeResource(conn, preparedStatement, resultSet);
} catch (SQLException e) {
e.printStackTrace();
}
return userList;
}
}
执行结果
User{id=1, name='admin', password='123456', email='1@qq.com', birthday=2020-09-29}
User{id=2, name='李四', password='111111', email='li@163.com', birthday=2020-09-27}
User{id=3, name='tom', password='tom', email='tom@qq.com', birthday=2008-01-01}
User{id=4, name='lyh', password='liuyuhe', email='104@qq.com', birthday=2020-09-07}
User{id=5, name='李四', password='111111', email='li@163.com', birthday=2020-09-27}
User{id=6, name='李四', password='111111', email='li@163.com', birthday=2020-09-27}
==========
User{id=2, name='李四', password='111111', email='li@163.com', birthday=2020-09-27}
==========
1
==========
1
==========
1
==========
User{id=1, name='admin', password='123456', email='1@qq.com', birthday=2020-09-29}
User{id=2, name='李四', password='111111', email='li@163.com', birthday=2020-09-27}
User{id=3, name='tom', password='tom', email='tom@qq.com', birthday=2008-01-01}
User{id=5, name='李四', password='111111', email='li@163.com', birthday=2020-09-27}
User{id=6, name='李四', password='111111', email='li@163.com', birthday=2020-09-27}
User{id=7, name='李四', password='111111', email='li@163.com', birthday=2020-09-27}
Process finished with exit code 0
蒟蒻写博客不易,加之本人水平有限,写作仓促,错误和不足之处在所难免,谨请读者和各位大佬们批评指正。
如需转载,请署名作者并附上原文链接,蒟蒻非常感激
名称:虐猫人薛定谔
博客地址:https://liuyuhe.blog.csdn.net/