Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 204
  • Last Modified:

can not insert into sql server database using jdbc driver

Hi
Im trying to insert a value into database but it is not insert

The value is mike's and the sql string is:

insert into per values ('mike's', 'people');

I know that is because it has ' but somebody know how to solve it?
0
olgavillamizar
Asked:
olgavillamizar
1 Solution
 
CEHJCommented:
Try

insert into per values ('mike''s', 'people');

Make sure you close the db resources afterwards
0
 
olgavillamizarAuthor Commented:
it doesn work....
also i get the data from another database
0
 
olgavillamizarAuthor Commented:
im using jdbc driver for sql server
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
CEHJCommented:
Use a PreparedStatement

ps.setString(1, "mike's");
ps.setString(2, "people");
ps.executeUpdate();
0
 
CEHJCommented:
You would precede that with


String sql = "insert into per values (?, ?)";
PreparedStatement ps = connection.prepareStatement(sql);
0
 
objectsCommented:
try:

insert into per values ('mike\'s', 'people');
0
 
olgavillamizarAuthor Commented:
how i do in java to identify if the value has ', because i get the values from another table and I do a for to insert it
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> how i do in java to identify if the value has '

You can use indexOf ():

int i = yourString.indexOf ( "'" ) ;

if ( i >= 0 )
  // it is present

You can use replaceAll () to replace ' with \'

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#replaceAll(java.lang.String,%20java.lang.String)
0
 
CEHJCommented:
>>how i do in java to identify if the value has ', because i get the values from another table and I do a for to insert it

You don't need to if you use a PreparedStatement (which will also give you enhanced performance for repeated inserts) as it handles all quoting for you
0
 
CEHJCommented:
:-)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now