姬長信(Redy)

java – 将参数传递给JDBC PreparedStatement


我正在尝试为我的程序制作验证课程.我已经建立了与MySQL数据库的连接,我已经在表中插入了行.该表由firstName,lastName和userID字段组成.现在我想通过构造函数的参数在数据库中选择一个特定的行.
import java.sql.*;
import java.sql.PreparedStatement;
import java.sql.Connection;

public class Validation {

    private PreparedStatement statement;
    private Connection con;
    private String x, y;

    public Validation(String userID) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/test", "root", "");
            statement = con.prepareStatement(
                    "SELECT * from employee WHERE  userID = " + "''" + userID);
            ResultSet rs = statement.executeQuery();
            while (rs.next()) {
                x = rs.getString(1);
                System.out.print(x);
                System.out.print(" ");
                y = rs.getString(2);
                System.out.println(y);
            }
        } catch (Exception ex) {
            System.out.println(ex);
        }
    }
}

但它似乎不起作用.