JDBC如何在带有where子句的预准备语句中使用占位符?

时间:2014-07-19 11:44:50

标签: java mysql jdbc prepared-statement

我很难理解为什么这不会起作用,如果我直接在MySQL控制台中键入完全相同的东西它会接受它,但是当我尝试运行它时,它会报告语法错误。

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:您的SQL语法中有错误;检查与您的MySQL服务器版本相对应的手册,以便在' =' 6'''在第1行

我尝试做的就是接收行中的数据,其中包含用户输入的member_id值。为了测试目的,值总是6,我尝试将其解析为int而不是字符串,这给出了相同的错误,我尝试将ID变量添加到字符串的末尾而不是使用占位符,但它没有&也很喜欢。

以下是代码:

public class MemberDAO {

public PreparedStatement ps = null;
public Connection dbConnection = null;

public List<Member> getMembersDetails(String ID) throws SQLException{

    List<Member> membersDetails = new ArrayList();

    String getMembershipDetails = "SELECT first_name, last_name, phone_number, email, over_18, date_joined, date_expire, fines FROM members"
            + "WHERE member_id = ?";

    try {
        DBConnection mc = new DBConnection();
        dbConnection = mc.getConnection();
        ps = dbConnection.prepareStatement(getMembershipDetails);

        ps.setString(1, ID);
        ps.executeQuery();

        ResultSet rs = ps.executeQuery(getMembershipDetails);


        String firstName = rs.getString("first_name");
        String lastName = rs.getString("last_name");
        String phoneNumber = rs.getString("phone_number");
        String email = rs.getString("email");
        String over18 = rs.getString("over_18");
        String dateJoined = rs.getString("date_joined");
        String dateExpired = rs.getString("date_expire");
        String fines = rs.getString("fines");

        Member m;
        m = new Member(firstName, lastName, phoneNumber, email, over18, dateJoined, dateExpired, fines);
        membersDetails.add(m);


    }  catch (SQLException ex){
        System.err.println(ex);
        System.out.println("Failed to get Membership Details.");
        return null;
    } finally{
        if (ps != null){
            ps.close();
        }
        if (dbConnection != null){
            dbConnection.close();
        }
    } return membersDetails;
}

这就是所谓的:

private void btnChangeCustomerActionPerformed(java.awt.event.ActionEvent evt) {                                                  

    customerID = JOptionPane.showInputDialog(null, "Enter Customer ID.");

    MemberDAO member = new MemberDAO();
    try {
        List membersDetails = member.getMembersDetails(customerID);

        txtFullName.setText(membersDetails.get(0) + " " + membersDetails.get(1));
    } catch (SQLException ex) {
        System.err.println(ex);
        System.out.println("Failed to get Details.");
        JOptionPane.showMessageDialog(null, "Failed to retrieve data.");
    }
}    

赞赏任何意见。

2 个答案:

答案 0 :(得分:1)

您的查询缺少空格:

...fines FROM members"
        + "WHERE...

将导致

 FROM membersWHERE

哪个是无效的SQL

将其更改为

 + " WHERE....

答案 1 :(得分:0)

您的查询是:

SELECT first_name, last_name, phone_number, email, over_18, date_joined, date_expire, fines FROM 

members WHERE member_id = ?

所以在成员之间和你需要空白字符的地方

SELECT first_name, last_name, phone_number, email, over_18, date_joined, date_expire, fines FROM members WHERE member_id = ?