Java-JDBC操作

来源:互联网 发布:怎样成为网络歌手 编辑:程序博客网 时间:2024/05/16 02:06

一、不使用注入方法

public void login(String username, String password) throws ClassNotFoundException, SQLException {        // 1.注册驱动        Class.forName("com.mysql.jdbc.Driver");        // 2.获取连接        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/数据库名", "root", "root");        // 3.创建执行sql语句的对象        Statement stmt = conn.createStatement();        // 4.书写一个sql语句        String sql = "select * from tbl_user where " + "uname='" + username + "' and upassword='" + password + "'";        // 5.执行sql语句        ResultSet rs = stmt.executeQuery(sql);        // 6.对结果集进行处理        if (rs.next()) {            System.out.println("恭喜您," + username + ",登录成功!");            System.out.println(sql);        } else {            System.out.println("账号或密码错误!");        }        //7.关闭        if (rs != null)            rs.close();        if (stmt != null)            stmt.close();        if (conn != null)            conn.close();    }

二、使用注入方法

public void login1(String username, String password) throws ClassNotFoundException, SQLException {        // 1.注册驱动        Class.forName("com.mysql.jdbc.Driver");        // 2.获取连接        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/数据库名", "root", "root");        // 3.编写sql语句        String sql = "select * from tbl_user where uname=? and upassword=?";        // 4.创建预处理对象        PreparedStatement pstmt = conn.prepareStatement(sql);        // 5.设置参数(给占位符)        pstmt.setString(1, username);        pstmt.setString(2, password);        // 6.执行查询操作        ResultSet rs = pstmt.executeQuery();        // 7.对结果集进行处理        if (rs.next()) {            System.out.println("恭喜您," + username + ",登录成功!");            System.out.println(sql);        } else {            System.out.println("账号或密码错误!");        }        //8.关闭        if (rs != null)            rs.close();        if (pstmt != null)            pstmt.close();        if (conn != null)            conn.close();    }
原创粉丝点击