2013-07-25 73 views
0

嗨,我有一个小问题,如何切换表以获取结果? 下面的代码不起作用。但是,这应该给你一些我想要做的事情的想法。 感谢您的帮助如何更改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 
+0

从您的错误消息:''fromspmovy_admin其中...'看起来像你错过了'from'和你的表名之间的空白。确保你在所有方法中都以正确的方式做到这一点。 –

+0

@LuiggiMendoza好,这是什么错了非常感谢你,请回答确切的事情在这里,所以我可以选择这个作为答案,非常感谢 –

回答

3

从你的错误信息:'fromspmovy_admin where ...看起来像你错过了from和你的表名之间的空白。确保你在所有方法中以正确的方式执行此操作(请注意,在当前的示例中,这不会发生)。

0

如果您将使用?为了传递变量,你必须使用PreparedStatement而不是Statement。同样根据你的错误信息,你需要从(从pmpmovy_admin中检查)后添加一个空白空间

+0

我正在使用准备好的声明好吧,我编辑了我正在使用的完整代码pls看一看。谢谢 –

相关问题