嗨,我有一个小问题,如何切换表以获取结果? 下面的代码不起作用。但是,这应该给你一些我想要做的事情的想法。 感谢您的帮助如何更改PreparedStatement中from子句的表名称
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其中...'看起来像你错过了'from'和你的表名之间的空白。确保你在所有方法中都以正确的方式做到这一点。 –
@LuiggiMendoza好,这是什么错了非常感谢你,请回答确切的事情在这里,所以我可以选择这个作为答案,非常感谢 –