JDBC Problem in Inserting ' Data
Hi guys....please give a hand on my problem here.
There is 1 table(named employee) in my Access 2000 consist of field NAME,SALARY AND JOB DESCRIPTION. By using the ResultSet in trying to insert into this table(assume not data at all ). Inserting the data will worked fine until i'm trying to insert data which have ' in the JOB DESCRIPITION. (example of the data --> SOFT'S ENGINEER). The ' symbols caused my sql statement terminate earlier/uncomplete. How can i inserting data like this..(i don't want to replace the ' symbol...still want to insert it)
The code is here...
String InsertTo = "insert into employess values ( ";
ResultSet set = statement.executeQuery(InsertTo + Name + "," SALARY + ",'" + JOBDESCRIPTION + " ' " + ")";
[789 byte] By [
pakadol] at [2007-9-26 1:36:30]

Hi,
A PreparedStatement is used to insert data containing QUOTES
PreparedStatement stmt = null;
String sql;
int rows;
try {
sql = "INSERT INTO tCust"
+ "(custName) "
+ "VALUES "
+ "(?)";
stmt = theConn.dbConn.prepareStatement(sql);
stmt.setString(1, "Name with \" are permitted!");
rows = stmt.executeUpdate();
theConn.dbConn.commit();
stmt.close();
System.out.println(sql);
}
catch (Exception e){
e,printStackTrace();
}
I hope this will give some idea.
Thanks
Bakrudeen
Hi,
>>(example of the data --> SOFT'S ENGINEER)
What about replacing the above string by ,
SOFT\'s ENGINEER. Its better you make a general function which accepts these type of strings and returns a String after inserting the escape syntax.
Otherwise, a prefered is to use PreparedStatement instead of Statement. Use pstmt.setString() and the jdbc driver will send the data in the required destination format.Hope this help..
regards Stallon
I cannot using prepared statement because my jdbc bridge version doesn't support it..
However..i found another way...
In Access and Oracle...if u going to put ' symbol...you have to put ' symbol two time in you statement.Means...
' in String equals to '' in sql statement.So you have to detect whether you InsertTo have ' symbol. If yes..the add 1 more ' symbol before/after the ' location to make it be ' ' before you can succesfully execute the statement.executeQuery(InsertTo)