package util;
import java.sql.*;
public class DBUtil {
private static final String URL = "jdbc:mysql://localhost:3306/imooc";
private static final String USER = "root";
private static final String PASSWORD = "123";
private static Connection connection = null;
static{
try {
Class.forName("com.mysql.cj.jdbc.Driver");
connection = DriverManager.getConnection(URL, USER, PASSWORD);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static Connection getConnection(){
return connection;
}
}
package dao;
import model.Goddess;
import util.DBUtil;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class GoddessDao {
public void addGoddess(Goddess goddess) throws SQLException {
Connection connection = DBUtil.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("insert into imooc_goddess(user_name, sex, age, birthday, email, mobile, create_user, create_date, update_user, update_date, isdel) values(?,?,?,?,?,?,?, current_date(), ?, current_date(), ?)");
preparedStatement.setString(1, goddess.getUser_name());
preparedStatement.setInt(2, goddess.getSex());
preparedStatement.setInt(3, goddess.getAge());
preparedStatement.setDate(4, new Date(goddess.getBirthday().getTime()));
preparedStatement.setString(5, goddess.getEmail());
preparedStatement.setString(6, goddess.getMobile());
preparedStatement.setString(7, goddess.getCreate_user());
preparedStatement.setString(8, goddess.getUpdate_user());
preparedStatement.setInt(9, goddess.getIsDel());
preparedStatement.execute();
}
public void updateGoddess(Goddess goddess) throws SQLException {
Connection connection = DBUtil.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("update imooc_goddess set user_name=?, sex=?, age=?, birthday=?, email=?, mobile=?, update_user=?, update_date=current_date(), isdel=? where id=?");
preparedStatement.setString(1, goddess.getUser_name());
preparedStatement.setInt(2, goddess.getSex());
preparedStatement.setInt(3, goddess.getAge());
preparedStatement.setDate(4, new Date(goddess.getBirthday().getTime()));
preparedStatement.setString(5, goddess.getEmail());
preparedStatement.setString(6, goddess.getMobile());
preparedStatement.setString(7, goddess.getUpdate_user());
preparedStatement.setInt(8, goddess.getIsDel());
preparedStatement.setInt(9, goddess.getId());
preparedStatement.execute();
}
public void delGoddess(int id) throws SQLException {
Connection connection = DBUtil.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("delete from imooc_goddess where id=?");
preparedStatement.setInt(1, id);
preparedStatement.execute();
}
public List<Goddess> queryAll() throws SQLException {
Connection connection = DBUtil.getConnection();
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("select user_name, age from imooc_goddess");
List<Goddess> goddessList = new ArrayList<>();
while(resultSet.next()){
Goddess goddess = new Goddess();
goddess.setUser_name(resultSet.getString("user_name"));
goddess.setAge(resultSet.getInt("age"));
goddessList.add(goddess);
}
return goddessList;
}
public Goddess getById(int id) throws SQLException {
Connection connection = DBUtil.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("select user_name, age from imooc_goddess where id=?");
preparedStatement.setInt(1, id);
ResultSet resultSet = preparedStatement.executeQuery();
Goddess goddess = null;
while (resultSet.next()){
goddess = new Goddess();
goddess.setId(resultSet.getInt("id"));
goddess.setUser_name(resultSet.getString("user_name"));
goddess.setSex(resultSet.getInt("sex"));
goddess.setAge(resultSet.getInt("age"));
goddess.setBirthday(resultSet.getDate("birthday"));
goddess.setEmail(resultSet.getString("email"));
goddess.setMobile(resultSet.getString("mobile"));
goddess.setCreate_user(resultSet.getString("create_user"));
goddess.setUpdate_user(resultSet.getString("update_user"));
goddess.setCreate_date(resultSet.getDate("create_date"));
goddess.setUpdate_date(resultSet.getDate("update_date"));
goddess.setIsDel(resultSet.getInt("isdel"));
}
return goddess;
}
}