Insering Items with ' into a DB!

Hello all:

I am facing a tiny problem. I have a string to Insert into the database. The string contains a '. (say today's).

Class.forName(driver_name).newInstance();

Connection connectiondB = DriverManager.getConnection("jdbc:odbc:DBASE");

Statement statementdB = connectiondB.createStatement();

int j = statementdB.executeUpdate("INSERT into Table1 values('"+value1+"','"+value2+"')");

Suppose these are the values for a particular case...

value1 = today's

value2 = XYZ

If I'm not wrong this is the way we insert values into a DBase!

When I execute it for a value which is today's...it throws an exception

java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Syntax error (missing operator) in query expression ' 'today's','XYZ')'.

Any ideas of what I can do to prevent this!

Thanks in advance

Prashanth

[938 byte] By [varaprashanth] at [2007-9-26 4:06:19]
# 1
I am not familiar with DBASE but ignorance has never stopped me from voicing an opnion.:)I have 2 suggestions for you to try1) change today's to today''s -- 2 single quotesor2) change today's to "today's" -- put string in double quotes.good luck
pendalama at 2007-6-29 13:06:30 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2

use PreparedStatement:

Class.forName(driver_name).newInstance();

Connection connectiondB = DriverManager.getConnection("jdbc:odbc:DBASE");

PreparedStatement statementdB = connectiondB.prepareStatement("INSERT into Table1 values(?, ?)");

statementDB.setString(1, "today's");

statementDB.setString(2, "XYZ");

int j = statementdB.executeUpdate();

Something like that.

Scott

sfrenkiel at 2007-6-29 13:06:30 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3

Hi there!

It's really a tiny problem: you have a variable containing simple quotes. Use the following before using your variable in the sql query.

Value1 = String.replace( Value1 , "'" , "'''" ) ;

You just have to replace every simple quote with triple-quote!

AND it works.

Bye folks!

sylvain.barbot at 2007-6-29 13:06:30 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...