将数据从html插入数据库,查询错误

时间:2013-05-13 20:37:57

标签: java sql database jsp

从html页面将数据插入数据库,我已成功将数据传递给jsp,然后传递给java文件,但是在将数据插入数据库时​​出现错误。

这是查询:

String ResultQuery = "INSERT INTO Results (homeTeam, awayTeam, homeScore, awayScore)" +
    "VALUES (+HomeTeam+','+AwayTeam+','+HomeScore+','+AwayScore+)";

这是错误:

javax.servlet.ServletException: java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Syntax error (missing operator) in query expression '+HomeTeam+','+AwayTeam+','+HomeScore+','+AwayScore+'.

3 个答案:

答案 0 :(得分:2)

您的查询错误,如果所有这些[HomeTeam,AwayTeam,HomeScore,AwayScore]都是变量,您应该以这种形式重写查询:

String ResultQuery = "INSERT INTO Results (homeTeam, awayTeam, homeScore, awayScore)" +
    "VALUES ('"+HomeTeam+"','"+AwayTeam+"','"+HomeScore+"','"+AwayScore+"')";

但如果那些不是变量,你应该用这种形式写:

String ResultQuery = "INSERT INTO Results (homeTeam, awayTeam, homeScore, awayScore)" +
    "VALUES ('HomeTeam','AwayTeam','HomeScore','AwayScore')";

答案 1 :(得分:0)

您似乎在第一个值之前缺少单个引号('),然后在最后一个值结束时丢失。

答案 2 :(得分:0)

看起来你错过了一些引用 - 试试这个:

String ResultQuery = "INSERT INTO Results (homeTeam, awayTeam, homeScore, awayScore)" +
"VALUES ("+HomeTeam+"','"+AwayTeam+"','"+HomeScore+"','"+AwayScore+")";

(我假设HomeTeam, AwayTeam, HomeScore, AwayScore是变量)