2014-01-18 25 views
0

的Java + MySQL錯誤我編程的Java和Eclipse正顯示出我這個錯誤:上查詢

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 '''where Name=''and Password=''' at line 1 

我試圖修復查詢,但錯誤remains.Can你控制,如果我正確地編寫查詢?我想創建這個類不重複所有類中的連接。但這不是錯誤。

text1,2,3是文本框。我想通過文本框3.

String name = text1.getText(); 

String password = text2.getText(); 

String employment = text3.getText(); 

try { 

ResultSet rs = Connection.Creation().executeQuery("select * from '" + employment + 
"'where Name='" + name + "'and Password='" + password + "'"); 
} 
catch 
{ 
//something here 
} 
+0

這樣你就非常容易受到SQL注入的攻擊。 – Rob

+0

您正在使用Java字符串'employment'的值來命名您的表。這是你的意圖嗎? –

+0

正是,我想探索給出的輸入表 – OiRc

回答

0
Try this 

MySQL Identifiers 

The identifier quote character is the backtick ("`") in MySQL. 

ResultSet rs = Connection.Creation().executeQuery("select * from `" + employment + 
"` where Name='" + name + "'and Password='" + password + "'"); 
0
ResultSet rs = Connection.Creation().executeQuery(" 
select * from " + employment + " where Name='" + name + "' and Password='" + password + "'"); 

的距離不應該有'拿到表名。

正如我在評論中所說的,這種方式對SQL注入非常脆弱。

+0

與您的更正我有同樣的錯誤。 – OiRc

+0

我已經更新了我的答案,請再次複製它並向我顯示您遇到的錯誤。 – Rob

+0

相同的錯誤..完整的顯示錯誤是com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:您的SQL語法中有錯誤;檢查與您的MySQL服務器版本相對應的手冊,在第1行'where Name =''和Password ='''附近使用正確的語法 – OiRc