0

J2ME で動作するようにサーブレットを作成していますが、これを実行するたびにエラー メッセージが表示されます。

エラー:

java.sql.SQLException: [Microsoft][ODBC SQL Server Driver][SQL Server]Line 1: 'password' 付近の構文が正しくありません。)

public class GetNpostServlet extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res) 
                     throws ServletException, IOException
{
// Same code appears in doPost()
// Shown both places to emphasize that data is received thru
// different means (environment variable vs stream), 
// yet processed the same inside the servlet
String acct = req.getParameter("account"),
        pwd = req.getParameter("password");    

String balance = accountLookup(acct, pwd);

if (balance == null)
{
  res.sendError(HttpServletResponse.SC_BAD_REQUEST, 
 "Unable to locate  account.");            
  return;
}

res.setContentType("text/plain");    
PrintWriter out = res.getWriter();
out.print(balance);
out.close();
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse res) 
                    throws ServletException, IOException
{
// Same code appears in doGet()
// Shown both places to emphasize that data is received thru
// different means (stream vs environment variable), 
// yet processed the same inside the servlet
String acct = req.getParameter("account"),
        pwd = req.getParameter("password");    

String balance = accountLookup(acct, pwd);

if (balance == null)
{
  res.sendError(HttpServletResponse.SC_BAD_REQUEST, 
"Unable to locate account.");            
  return;
}

res.setContentType("text/plain");    
PrintWriter out = res.getWriter();
out.print(balance);
out.close();
}

/*--------------------------------------------------
 * Lookup bank account balance in database
 *-------------------------------------------------*/
private String accountLookup(String acct, String pwd)
{
Connection con = null;
Statement st = null;
StringBuilder msgb = new StringBuilder("");

try
{
  // These will vary depending on your server/database      
  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  
  con = DriverManager.getConnection("jdbc:odbc:acctInfo");

  Statement stmt = con.createStatement();
  ResultSet rs = stmt.executeQuery(
                     "Select balance from acctInfo where account = " +
                     acct + "and password = '" + pwd + "'");      

  if (rs.next())
    return rs.getString(1);
  else
    return null;
   }
   catch (Exception e)
   {                  
    return e.toString();
    }
    }

    }
4

1 に答える 1

5

最も差し迫った問題: 文字列を引用しておらず、その後acctにスペースがないため、次のような結果になります:

Select balance from acctInfo where account = fredand password = 'bloggs'

もっと重要な問題: そもそも、このように SQL に値を含めるべきではありません。パラメータ化された SQL を使用する必要があります。現在、コードはSQL インジェクション攻撃に対して無防備です。

さらなるセキュリティ上の問題: あなたのコードは、パスワードがプレーン テキストで保持されていることを示唆しています。これをしないでください。

設計上の問題:

  • 口座残高を文字列として扱っているようです。それは変です。BigDecimalより自然な表現になります。
  • Exception特定の例外ではなくキャッチしています
  • あたかも口座残高であるかのように、例外文字列表現を返しています。うわぁ!おそらく実際にSQLExceptionは、発信者にバブルアップさせる必要があります
  • 接続、ステートメント、または結果セットを閉じることはありません
于 2012-05-08T09:05:06.237 に答える