java prepareStatement的使用
当我执行下面的代码时:打印的结果是
0------name
1------name
2------name
3------name
4------name
小弟不明白,为什么没有打印真正的用户的名字,而是“name”?
public class Test {
public static void main(String[] args) {
Connection con = null;
try {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//建立连接
con = DriverManager.getConnection(
"jdbc:mysql://localhost/wms_flex_db", "root", "root");
//创建语句
PreparedStatement st = con
.prepareStatement("select ? from tb_person ");
//设置
st.setString(1, "name");
//执行语句
ResultSet sets = st.executeQuery();
int count = 0;
while (sets.next()) {
System.out.print(count + "------" + sets.getString(1));
System.out.println();
count++;
}
} catch (
ClassNotFoundException e) {
e.printStackTrace();
} catch (
SQLException e) {
e.printStackTrace();
} finally {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
------解决方案--------------------
------解决方案--------------------Java code
PreparedStatement st = con.prepareStatement("select name from tb_person ");
// 条件才可以用setXXX()
------解决方案--------------------