0

I am trying to get some information of a website into an access database. I am creating a bean out of the information I get of the website and then send that bean into the database. the problem is that I've been blocked getting certain things into the database by an exception. the exception -

net.ucanaccess.jdbc.UcanaccessSQLException: unexpected token: (token) required:

so ive checked whats in common with the values that are generating this exception. its an apostrophe. Every value that is calling an exception has an apostrophe in it and I can't really change it for now. so I've been wondering how do I make ucanaccess get that apostrophe into access without any exceptions?

this is the query statement

stmnt = conn.prepareStatement("INSERT INTO Table1(doctorName , description , specialty1 , specialty2 , personalSite , clinic1 , phone1 , clinic2 , phone2 , clinic3, phone3 ,worksWith) VALUES (?,?,?,?,?,?,?,?,?,?,?,?");  

            stmnt.setString(1,tempBean.getDoctorName());
            stmnt.setString(2,tempBean.getDescription());
            stmnt.setString(3,tempBean.getSpeciality1());
            stmnt.setString(4,tempBean.getSpeciality2());
            stmnt.setString(5,tempBean.getPersonalSite());
            stmnt.setString(6,tempBean.getClinic1());
            stmnt.setString(7,tempBean.getPhone1());
            stmnt.setString(8,tempBean.getClinic2());
            stmnt.setString(9,tempBean.getPhone2());
            stmnt.setString(10,tempBean.getClinic3());
            stmnt.setString(11,tempBean.getPhone3());
            stmnt.setString(12,tempBean.getWorksWith());

            stmnt.executeUpdate();
4

2 回答 2

2

您遇到了一个常见的问题,称为 sql 注入。

使用 Java 确保不会发生这种情况的方法是使用 PreparedStatements。从另一个问题中查看这个答案作为一个很好的例子。

于 2015-03-17T21:37:14.053 回答
1

对于未来的读者:

您应该使用 PreparedStatement 来避免 SQLInjection 并避免需要转义文本值。在 SQL(带有所有 dbms 和所有驱动程序)中,在这种特定情况下,您应该使用另一个撇号来转义撇号:

单词 xxx'xxx 应该转义为 xxx''xxx。

因为 UCanAccess 支持 Access 语法,所以当使用双引号 " 分隔符而不是撇号时同样有效(在这种情况下,单词中间的双引号必须用另一个双引号 " 进行转义)。
如果不是,sql 引擎将无法知道撇号是文本分隔符还是单词的一部分。但是如果你使用 PreparedStatement,你就不用担心了。

于 2015-03-17T23:15:57.750 回答