插入文本对于某些字符串值失败,但对其他字符串值失败 [英] Inserting text fails for some string values but not for others

查看:146
本文介绍了插入文本对于某些字符串值失败,但对其他字符串值失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  String addQuer =INSERT INTO emaildata VALUES('+ email +','+ fname +','+ lname + ','+ content +'); 
statement.execute(addQuer);

我有上面的代码插入数据到表中。我使用MS Access数据库。所有字段都是类型(长文本)。在插入以下数据集时,查询效果很好


  1. email = SALMAN MAJID



  2. 测试邮件

  3. mY nAME iS sALMAN。

但是,当我在最后一个字段中使用以下数据集时,同一个查询会给出错误。错误是语法错误丢失操作符....请不要介意这样长的文本,因为这个文本给出错误所以我张贴的整个文本。

  java.sql.SQLException:[Microsoft] [ODBC Microsoft Access Driver]查询表达式中的语法错误(缺少操作符)获取免费的Outlook应用程序。 

我们升级了Outlook应用程序更快更容易使用。


  1. Outlook.com

  2. 不可用

  3. 不可用

  4. 立即下载:Outlook应用程式

  5. 取得免费的Outlook应用程式。 p>

    我们升级了Outlook应用程序,使其更快速,更易于使用。无论您使用哪种电子邮件服务,Outlook应用程式都可让您执行更多操作。


[下载Outlook] $ b http://communication.microsoft.com/ Key-6859501.C.ChVBd.C.K4 .-。HJ67kt



字段5结束于此。上面的文本是长的,但我不能粘贴整个文本。但是错误是在我发布的这个文本。

解决方案

你正在遭受SQL注入问题。包含单引号的文字(例如,We've ...)会导致您的SQL命令无效。您需要使用参数化查询,其格式如下:

  String addQuer =INSERT INTO emaildata VALUES(?,?,?,?,?); 
PreparedStatement ps = conn.prepareStatement(addQuer);
ps.setString(1,email);
ps.setString(2,fname);
ps.setString(3,lname);
ps.setString(4,subject);
ps.setString(5,content);
ps.executeUpdate();


String addQuer = "INSERT INTO emaildata VALUES('" + email + "','" + fname + "','" + lname + "','" + subject + "','" + content + "')";
        statement.execute(addQuer);

I have the above code that inserts data into the table. I am using MS Access database. All the fields are of type (Long Text). The query works fine when i insert the following dataset

  1. email = SALMAN MAJID
  2. Salman
  3. Majid
  4. Test Mail
  5. mY nAME iS sALMAN.

But the same query gives error when i use the following data set in the last field. The error is Syntax error Missing Operator.... Please don't mind for such long text because this text gives the error so i posted the whole text.

java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Syntax error (missing operator) in query expression ''Get the free Outlook app.

We've upgraded the Outlook app to be faster and easier to use. No matter which email services you use'.

  1. "Outlook.com"
  2. Not Available
  3. Not Available
  4. Download Now: Outlook app
  5. Get the free Outlook app.

    We've upgraded the Outlook app to be faster and easier to use. No matter which email services you use, the Outlook app lets you do more.

[Download Outlook.] http://communication.microsoft.com/Key-6859501.C.ChVBd.C.K4.-.HJ67kt

The field 5 ends here. The above text is long but i could not paste the whole text. But the error is in this text that i posted.

解决方案

You are suffering from "SQL injection" problems. Text containing a single quote (e.g., "We've ...") will cause your SQL command to be invalid. You need to use a parameterized query, which would look something like this:

String addQuer = "INSERT INTO emaildata VALUES (?,?,?,?,?)";
PreparedStatement ps = conn.prepareStatement(addQuer);
ps.setString(1, email);
ps.setString(2, fname);
ps.setString(3, lname);
ps.setString(4, subject);
ps.setString(5, content);
ps.executeUpdate();

这篇关于插入文本对于某些字符串值失败,但对其他字符串值失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆