App(4.23)

今天依旧是数据库

public class DBOpenHelper {
    private static String driver = "com.mysql.jdbc.Driver";
    private static String url = "jdbc:mysql://localhost:3306/test?characterEncoding=utf-8";
    private static String user = "root";//用户名
    private static String password = "root";//密码

    public static Connection getConn(){
        Connection conn = null;
        try {
            Class.forName(driver);
            conn = (Connection) DriverManager.getConnection(url,user,password);//获取连接
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

    //登录的查询
    public static boolean loginByAccount(String username,String password) {
        Connection conn = getConn();
        String sql = "select *from account where username = ? and password = ? ";
        if (conn != null) {//进行查询是否有该用户
            try {
                PreparedStatement ps = conn.prepareStatement(sql);
                ps.setString(1,username);
                ps.setString(2,password);

                ResultSet rs = ps.executeQuery();
                if (rs != null) {
                    return true;
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
        return false;
    }
}
原文地址:https://www.cnblogs.com/ywqtro/p/12764573.html