嗨,我有一個小問題,如何切換表以獲取結果? 下面的代碼不起作用。但是,這應該給你一些我想要做的事情的想法。 感謝您的幫助如何更改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好,這是什麼錯了非常感謝你,請回答確切的事情在這裏,所以我可以選擇這個作爲答案,非常感謝 –