driver = com.mysql.jdbc.Driver
url = jdbc:mysql://localhost:3306/jdbc?serverTimezone=Asia/Shanghai&?useUnicode=true&characterEncoding=utf8&useSSL=false
username = root
password = 1234
package com.jdbc;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class BaseDao {
private static String driver;
private static String url;
private static String username;
private static String password;
static {
Properties properties = new Properties();
InputStream resources = BaseDao.class.getClassLoader().getResourceAsStream("db.properties");
System.out.println(BaseDao.class.getClassLoader());
try {
properties.load(resources);
} catch (IOException e) {
e.printStackTrace();
}
driver = properties.getProperty("driver");
url = properties.getProperty("url");
username = properties.getProperty("username");
password = properties.getProperty("password");
System.out.println(driver);
System.out.println(url);
System.out.println(username);
System.out.println(password);
}
public static Connection getConnection(){
Connection connection = null;
try {
Class.forName(driver);
connection = DriverManager.getConnection(url, username, password);
} catch (Exception e) {
e.printStackTrace();
}
return connection;
}
public static ResultSet executeFind(Connection connection ,String sql ,Object[] params,ResultSet resultSet ,PreparedStatement preparedStatement) throws SQLException {
preparedStatement = connection.prepareStatement(sql);
for (int i = 0; i < params.length; i++) {
preparedStatement.setObject(i+1,params[i]);
}
resultSet = preparedStatement.executeQuery();
return resultSet;
}
public static int executeCRUD(Connection connection ,String sql ,Object[] params,PreparedStatement preparedStatement ) throws SQLException {
preparedStatement = connection.prepareStatement(sql);
for (int i = 0; i < params.length; i++) {
preparedStatement.setObject(i+1,params[i]);
}
int updateRows = preparedStatement.executeUpdate();
return updateRows;
}
public static boolean closeResource(Connection connection,PreparedStatement preparedStatement,ResultSet resultSet ){
boolean flag = true;
if(resultSet != null){
try {
resultSet.close();
resultSet = null;
} catch (SQLException e) {
flag = false;
e.printStackTrace();
}
}
if(preparedStatement != null){
try {
preparedStatement.close();
preparedStatement = null;
} catch (SQLException e) {
flag = false;
e.printStackTrace();
}
}
if(connection != null){
try {
connection.close();
connection = null;
} catch (SQLException e) {
flag = false;
e.printStackTrace();
}
}
return flag;
}
}
import com.pojo.User;
import java.sql.Connection;
public interface UserDao {
public User getLoginUser(Connection connection , String userCode);
}
package com.dao.user;
import com.dao.BaseDao;
import com.pojo.User;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class UserDaoImpl implements UserDao{
@Override
public User getLoginUser(Connection connection, String userCode) {
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
User user = null;
if(connection != null){
String sql = "select * from users where user_code = ? ";
Object[] params = { userCode };
try {
resultSet = BaseDao.executeFind(connection, preparedStatement, sql, params);
if(resultSet.next()){
user = new User();
user.setId(resultSet.getInt("id"));
user.setName(resultSet.getString("name"));
user.setUserCode(resultSet.getString("user_code"));
}
boolean resource = BaseDao.closeResource(connection, preparedStatement, resultSet);
System.out.println(resource);
} catch (SQLException e) {
e.printStackTrace();
}
}
return user;
}
public static void main(String[] args) {
UserDaoImpl userDao = new UserDaoImpl();
Connection connection = BaseDao.getConnection();
User user = userDao.getLoginUser(connection, "ad");
System.out.println(user);
}
}