在代码java中有语法错误

时间:2013-12-02 11:48:18

标签: java netbeans

为什么此代码中存在语法错误?

 String strSqlUpdate = "UPDATE Customers SET Contact = " + contact_num + "," 
          + "Email = '" + email_add + "'," 
          + "Address = '" + mail_add + "'," 
          + "SurveyStatus = " + radio_group + ","
          + "Subscription = " + receive_info + 
          "WHERE membership_ID = '" + member_ID'; 

我认为我的代码是对的。

2 个答案:

答案 0 :(得分:3)

如果是代码中的错误,请检查您使用的所有变量是否已使用适当的值进行声明和初始化。

如果是sql的语法困扰你,那么如果所有变量都被初始化为null,那么你的sql就是这样。

 UPDATE Customers SET (Contact)null,Emailnull,Address,null,SurveyStatus,null,SubscriptionnullWHERE MembershipID =null

使用 strSqlUpdate 中的空格来更正上述sql。

修改

你需要的是这样的东西。

String strSqlUpdate = "UPDATE Customers SET Contact = " + contact_num
              + ",Email = '" + email_add + "'" 
              + ",Address = '" + mail_add + "'" 
              + ",SurveyStatus = '" + radio_group + "'"
              + ",Subscription = '" + receive_info + "' " 
              + "WHERE membership_ID = '" + member_ID + "'";

答案 1 :(得分:1)

当我声明并初始化所有变量时,我没有语法错误。您必须确保它们在strSqlUpdate

的范围内全部初始化
String contact_num = "";
String email_add = "";
String mail_add = "";
String radio_group = "";
String receive_info = "";
String member_ID = "";
String strSqlUpdate = " UPDATE Customers SET (Contact)" + contact_num + "," + "Email"
            + email_add + "," + "Address" + "," + mail_add + "," + "SurveyStatus" + "," + radio_group
            + "," + "Subscription" + receive_info + "WHERE MembershipID =" + member_ID;

另外考虑到您正在谈论SQL语法,添加其他人所说的内容,我建议您应该使用PreparedStatement来避免SQL注入。

PreparedStatement pst = conn.prepareStatement(
                     "UPDATE Customers SET (Contact) ?, ?, ?, ?, ?, ?, ? WHERE ? = ?");
pst.setString(1, contact_num);
pst.setString(2, email_add);
... and so on

当前SQL语法中的错误是

"Subscription" + receive_info + "WHERE MembershipID

翻译为

"...Subscrptionreceive_infoWHERE MembershipID..."

您需要在没有逗号的地方添加空格