Java连接MySQL数据库>升级

Java连接MySQL数据库

  1. 配置文件
	ClassLoader classLoader = JDBCTools.class.getClassLoader();
	URL resource = classLoader.getResource("jdbc.properties");
	assert resource != null;
	String path = resource.getPath();
	prop.load(new FileReader(path));
	user = prop.getProperty("user");
	password = prop.getProperty("password");
	url = prop.getProperty("url");
	driver = prop.getProperty("driver");
	Class.forName(driver);
  1. 自己写的连接工具类
public class JDBCTools {
    
    
    private static String user;
    private static String password;
    private static String url;
    private static String driver;
    static {
    
    
        Properties prop = new Properties();
        try {
    
    
            ClassLoader classLoader = JDBCTools.class.getClassLoader();
            URL resource = classLoader.getResource("jdbc.properties");
            assert resource != null;
            String path = resource.getPath();
            prop.load(new FileReader(path));
            user = prop.getProperty("user");
            password = prop.getProperty("password");
            url = prop.getProperty("url");
            driver = prop.getProperty("driver");

            Class.forName(driver);
            

        } catch (IOException | ClassNotFoundException e) {
    
    
            e.printStackTrace();
        }

    }
    public static Connection getConnection() throws SQLException {
    
    
        return DriverManager.getConnection(url,user,password);
    }
    public static void close(ResultSet res,Statement stmt, Connection conn){
    
    
        if (res != null){
    
    
            try {
    
    
                res.close();
            } catch (SQLException throwables) {
    
    
                throwables.printStackTrace();
            }
        }
        if (stmt != null){
    
    
            try {
    
    
                stmt.close();
            } catch (SQLException throwables) {
    
    
                throwables.printStackTrace();
            }
        }
        if (conn != null){
    
    
            try {
    
    
                conn.close();
            } catch (SQLException throwables) {
    
    
                throwables.printStackTrace();
            }
        }
    }
}

  1. 防sql注入
private boolean login(String username, String password) {
    
    
     Connection conn = null;
     PreparedStatement pstmt = null;
     ResultSet resultSet = null;
     if (username == null || password == null){
    
    
         return false;
     }
     try {
    
    
         conn = JDBCTools.getConnection();
         String sql = "select * from user where `username`=? and `password` = ?";
         pstmt = conn.prepareStatement(sql);
         pstmt.setString(1,username);
         pstmt.setString(2,password);
         resultSet = pstmt.executeQuery();
         return resultSet.next();
     } catch (SQLException throwables) {
    
    
         throwables.printStackTrace();
     } finally {
    
    
         JDBCTools.close(resultSet,pstmt,conn);
     }
     return false;
 }

猜你喜欢

转载自blog.csdn.net/weixin_45867397/article/details/107822692