employee(id, name, company, salary);
需要对给定id
public static void Connect(String conString, String username, String password, int id) throws SQLException{
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = null;
conn = DriverManager.getConnection(conString, username, password);
String query = "select * from employee where id = " + id + "" ;
ResultSet rs = null;
Statement stmt = null;
stmt = conn.createStatement();
rs = stmt.executeQuery(query);
while(rs.next()){
String name = rs.getString("name");
String company = rs.getString("company");
int salary = rs.getInt("salary");
System.out.println("Name: " + name + "\tCompany: " + company + "\tSalary: " + salary);
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
但在这里我们直接传递ID显示数据。我们怎样才能通过它像参数化查询(像我们如何通过?
期间PreparedStatement
)
[将参数传递给JDBC PreparedStatement]可能重复(http://stackoverflow.com/questions/12745186/passing-parameters-to-a-jdbc-preparedstatement) – 2015-02-24 09:53:46