0
if(hidPass.equals(pass)){
            String encodedPass = Base64.encode(newPass.getBytes(), Base64.BASE64DEFAULTLENGTH);
            try{
                String connectionURL = "jdbc:mysql://localhost:3306/books";// books is the database  
                Connection connection=null;  
                Class.forName("com.mysql.jdbc.Driver");  
                  connection = DriverManager.getConnection(connectionURL, "root", "bonnysingh");  
                  Statement st = connection.createStatement();

                   st.executeUpdate("UPDATE signup SET password="+encodedPass+"WHERE Username="+CurrentUser);
                   System.out.println("Update Complete");
            }
            catch(Exception e){
                System.out.println(e);
                response.sendRedirect("view.jsp");
            }
        }
        else{
            System.out.println("Update InComplete");
        }

このエラーが発生しています

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: SQL 構文にエラーがあります。1 行目の 'WHERE Username=Damanpreet' 付近で使用する正しい構文については、MySQL サーバーのバージョンに対応するマニュアルを確認してください。

誰でも私を助けることができますか?

4

3 に答える 3

0

まず、 を使用する必要があります。そうしないと、メソッドがSQL インジェクション攻撃PreparedStatementを受けやすくなります。第 2 に、クエリに s を送信するときは、データベース エンジンによって、または データベース エンジンに応じて、それらを囲む必要があります。第 3 に、メモリ リークが発生しないように、使用後は必ずリソースを閉じる必要があります。この場合、 と を閉じる必要があります。String'"PreparedStatementConnection

これらのアドバイスに基づいてコードを調整すると、次のようになります。

Connection connection=null;
PreparedStatement pstmt = null;
try {
    String connectionURL = "jdbc:mysql://localhost:3306/books";// books is the database  
    Class.forName("com.mysql.jdbc.Driver");  
    connection = DriverManager.getConnection(connectionURL, "root", "bonnysingh");  
    String updateSql = "UPDATE signup SET password = ? WHERE Username = ?";
    pstmt = connection.prepateStatement(updateSql);
    pstmt.setString(1, encodedPass);
    pstmt.setString(2, CurrentUser);
    pstmt.executeUpdate();
    System.out.println("Update Complete");
} catch(Exception e) {
    //System.out.println(e);
    //handle the exception!
    e.printStackTrace();
} finally {
    if (pstmt != null) {
        try {
            pstmt.close();
        } catch (SQLException e) {
            e.printStacktrace();
        }
    }
    if (connection != null) {
        try {
            connection.close();
        } catch (SQLException e) {
            e.printStacktrace();
        }
    }
}
response.sendRedirect("view.jsp");
于 2013-05-30T16:42:00.203 に答える