嗨,我有一个小问题,我如何切换表来获取结果? 下面的代码不起作用。但是,这应该让您了解我正在尝试做什么。 感谢您的帮助
String typelogin=null;
if(xx){
typelogin="users_table";
}else{
typelogin="admin_table";
}
String sqlStr = "Select * from "+typelogin+" where username=? and userpassword=?";
PreparedStatement stmt = conn.prepareStatement(sqlStr);
完整代码:
Statement stmt = conn.createStatement();
String sqlStr = "Select * from "+typelogin+" where username=? and userpassword=?";
PreparedStatement pstmt=conn.prepareStatement(sqlStr);
pstmt.setString(1,user);
pstmt.setString(2,password);
//step 6 Process result
ResultSet rs = pstmt.executeQuery();
我收到的错误:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'fromspmovy_admin where username='abc' and userpassword='abc'' at line 1
回答[已解决]:
忘记加空格
from " + typelogin + " where
最佳答案
从您的错误消息来看:'fromspmovy_admin where ...
看起来您错过了 from
和表名称之间的空格。确保您在所有方法中都以正确的方式执行此操作(请注意,在当前的示例中,这种情况不会发生)。
关于java - 如何更改PreparedStatement中from子句的表名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17862990/