0

I am trying to store to my MySQL database the path of a file I uploaded to Tomcat. I have stored the path to a string named filepath, but when I execute the program, I get this error:

"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ':\Users\Nick\Desktop\bot.png)' at line 1 " A part of the code I used:

String fileName = fi.getName();
String contentType = fi.getContentType();
boolean isInMemory = fi.isInMemory();
long sizeInBytes = fi.getSize();

// Write the file
if( fileName.lastIndexOf("\\") >= 0 ){
    file = new File( filePath + 
    fileName.substring( fileName.lastIndexOf("\\"))) ;
}else{
    file = new File( filePath + 
    fileName.substring(fileName.lastIndexOf("\\")+1)) ;
}

I suppose the problem has to do with the format of the path, but I am pretty new to java, so I would really appreciate some help. Thank you in advance.

2
  • 2
    The code and stacktrace, without them we dont know how to help. Commented Sep 15, 2014 at 23:16
  • Code added as advised Commented Sep 15, 2014 at 23:22

2 Answers 2

2

Either use double backslashes to escape or forward slash, or better still prepared statements.

Sign up to request clarification or add additional context in comments.

5 Comments

Just curious... How would a prepared statement help?
I know what prepared statement is. My question was how it could help to deal with escaped characters.
They talk about escaping from SQL side. When assigning a text value to a Java String (to be bound to query parameter) it's still necessary to escape according to Java rules. So in this case OP has to do what you suggest in the beginning to your answer even if changing to prepared statement.
0
 path = path.replaceAll((char) 92 + "" + (char) 92, (char) 47 + "");

1 Comment

You could strengthen your answer by adding an explanation, even though the code probably is quite straightforward in this case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.