1

I have raw insert query like

insert into sample(id, name) values(1, 'text \\N\'); 

Getting SqlException while trying to insert via jdbc but the same insert query is working if I insert via mysql command prompt(console).

jdbc insert query is failing due to special characters("\N") in name field.

so how to overcome and insert the name with \N?

9
  • Which DB do you use? Commented Sep 28, 2018 at 12:18
  • why you have extra "\" after "\\N"? Commented Sep 28, 2018 at 12:21
  • I use mysql database Commented Sep 28, 2018 at 12:38
  • @Nawnit Sen: for some reason I am receiving name with \\N\ so my part is to process without any modification Commented Sep 28, 2018 at 12:39
  • @SasikumarMurugesan can you add the complete error message? And try "\\N\\" this might work Commented Sep 28, 2018 at 12:45

1 Answer 1

2

The cleanest approach is to not use a raw SQL query at all. If, as you've stated, you receive the name from some other process then it is presumably in a String variable (or property, or similar) so you can simply use a parameterized query to perform the insert:

// example data int theId = 1; String theName = "the name you received from somewhere else"; // PreparedStatement ps = conn.prepareStatement("INSERT INTO sample (id, name) VALUES (?, ?)"); ps.setInt(1, theId); ps.setString(2, theName); ps.executeUpdate(); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.