ResultSet无法与Java中的PreparedStatement一起使用

时间:2016-09-08 09:30:24

标签: java mysql

我正在尝试使用PreparedStatement运行参数化查询。使用execute()可以很好地运行查询,但是当我尝试使用带有executeQuery()的ResultSet运行查询时,它没有。

public static boolean doLogin(User user){
    Connection con = DbConnector.doConnect(); 

    PreparedStatement ps = null;
    String query = "SELECT * FROM user_details WHERE email = ? and password = ?";
    boolean status = false;
    try {
        ps = con.prepareStatement(query);

        ps.setString(1,user.getEmail());
        ps.setString(2,user.getPassword());
        ResultSet rs = ps.executeQuery();
        if(rs.next())
        status = true;


    } 
    catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        con.close();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return status;
}

1 个答案:

答案 0 :(得分:0)

使用此代码解决您的问题

/*  Name for this file "db.java"
 *  ----------------------------------------------------------------------
 */
package abcbook;

import java.sql.*;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class db {

    private final String database;
    private final String username;
    private final String password;

    public String sql;
    public ResultSet rs;
    public Statement st;

    public db() {

        this.database = "abcbook_aslam";
        this.username = "root";
        this.password = "";
    }

    public void setQuery(String q) {

        this.sql = q;
    }

    public boolean startDb() {
        JFrame frame = new JFrame("Error Database");

        boolean ok = false;
        try {

            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/" + this.database, this.username, this.password);
            System.out.println("Request DB connection from Db_Abc... ");

            conn.createStatement();
            st = conn.createStatement();
            ok = true;

        } catch (ClassNotFoundException ex) {
            System.out.println("Error StartDB 1 from Db_Abc" + ex.getMessage());
            JOptionPane.showMessageDialog(frame, "Database is error please check it", "Error", JOptionPane.ERROR_MESSAGE);
            ok = false;
        } catch (SQLException ex) {
            System.out.println("Error  StartDB 2 from Db_Abc" + ex.getMessage());
            JOptionPane.showMessageDialog(frame, "Database is error please check it", "Error", JOptionPane.ERROR_MESSAGE);
            ok = false;
        }
        return ok;
    }

    public boolean ifDataFound(String fq) {

        boolean ok = false;
        setQuery(fq);
        try {

            rs = st.executeQuery(sql);

            while (rs.next()) {
                ok = true;
            }

        } catch (SQLException ex) {
            System.out.println("Error ifDataFound " + ex.getMessage());
            ok = false;
        }
        return ok;
    }


}


/*  Name for this file "test.java"
 *  ----------------------------------------------------------------------
 */

package abcbook;

public class test {

    public static void main(String args[])
    {
        db n = new db();
        n.startDb();
        if(n.ifDataFound("SELECT * FROM table WHERE id = 'yourid' "))
        {
            System.out.println("Found");
        }
    }

}