2015-01-13 45 views
0

我在我的應用程序中使用JDBC通過Java連接到數據庫,但是結果應該與發出查詢時相同。SQL語法錯誤,準備好的語句,沒有詳細信息

我試圖驗證登錄,但有一些問題,這是我的錯誤:

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 '' at line 1

正如你所看到的,它並沒有真正告訴我任何東西。

對於那些你使用PHP的我寫了使用PDO

function conversion($connection, $username, $password) { 
    $statement = $connection->prepare("SELECT * FROM `forum_users` WHERE `username`=:name AND `password`= MD5(CONCAT(MD5(salt), MD5(:pass))"); 
    $statement->bindParam(":name", $username); 
    $statement->bindParam(":pass", $password); 
    return $statement->execute(); 
} 

快速轉換以下是一個Java

Connection connection = null; 
PreparedStatement statement = null; 
ResultSet results = null; 
try { 
    connection = getConnection(); 
    statement = connection.prepareStatement("" 
      + "SELECT * FROM `"+SQL_DB+"`.`forum_users`" 
      + " WHERE `username`=?" 
      + " AND `password`= MD5(CONCAT(MD5(salt), MD5(?))"); 

    statement.setString(1, username); 
    statement.setString(2, password); 
    results = statement.executeQuery(); 

    while(results.next()) { 
     System.out.println("Login success"); 
    } 
} catch(SQLException e) { 
    e.printStackTrace(); 
} 

值得一提的是,鹽的代碼在數據庫中的列,我m試圖改變我的登錄使用一個單一的查詢,而不是兩個不同的。

+0

我實際上嚴格來自Java背景,帶有一點PHP(這是我所有的SQL體驗的來源),你在說什麼? Java使用'+'連接,並且我在這裏閱讀了關於使用CONCAT函數的http://www.1keydata.com/sql/sql-concatenate.html。 – Hobbyist

回答

2

錯別字:

" AND `password`= MD5(CONCAT(MD5(salt), MD5(?))") 
        1  2 3 3  4 42 

注意在括號中的不匹配。 )在哪裏關閉#1?錯誤消息是正確的,但由於您的錯誤發生在查詢字符串的最後,因此沒有「稍後」文本顯示爲錯誤上下文。

+1

哦,耶穌,我怎麼能重新寫這三次缺少括號。 :facepalm:謝謝,這一切都是爲了現在。 – Hobbyist

+1

永不假定準備成功。始終使用數據庫操作(或涉及外部資源的任何操作)檢查失敗。 –