Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

My sql database storage 1

Status
Not open for further replies.

abyinsydney

Programmer
Feb 17, 2004
99
AU
greetings fellow citizens
I'm reading from a a text file and the same file has to be uploaded to mysql database.However mysql doesnot recognise ' symbol.How can you replace this character with some other character and store it in database.


please help
aby
 
The SQL needs to be like this :

insert into test values ('Somebody''s house');

or you can use PreparedStatement and bind varibales :

ps.setString(1,"Somebody's");
 
venu
i'm reading from the file and storing the characters in a string.

how do i replace the ' character with "" prepared statement will not work it only works with string values.

Is there some way i can handle this issue

aby
 
Hi,

In the question itself there is an answer. You are reading from a file and storing it in a String and Prepared Statement takes String.

Simple Example: Lest's assume EmapleArray is a File and every element is a Line or Character. I am appending every line to the String psmtString in the for loop. Once the loop is done it will be a complete String that want to insert into DB using preparedStatement.setString(1,pstmtString).

String[] ExampleArray = {
"Sun Solaris"
, "HP-UX"
, "Linux"
, "MS Windows"
, "Macintosh"
};
String psmtString="";
for(int i=0; i< ExampleArray.length; i++)
{
psmtString+=ExampleArray;
}
System.out.println(psmtString);

If you think the File is very big the use StringBuffer insted of String.

String[] ExampleArray = {
"Sun Solaris"
, "HP-UX"
, "Linux"
, "MS Windows"
, "Macintosh"
};
StringBuffer psmtString= new StringBuffer();
for(int i=0; i< ExampleArray.length; i++)
{
psmtString.append(ExampleArray);
}
System.out.println(psmtString.toString());

pstmt.setString(1,psmtString.toString());

Hope this will help you..

Cheers
Venu
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top