嗨,我有一个小问题,如何切换表格以获取结果?
下面的代码不起作用。但是,这应该使您对我正在尝试执行的操作有所了解。
谢谢您的帮助

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和表名之间的空格。确保在所有方法中都以正确的方式执行此操作(请注意,在当前示例中,这不会发生)。

10-05 21:51