2013-07-25 76 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看一看。謝謝 –

相關問題