Tuesday 25 September 2012

How to insert values with single quote in MySQL 5.1 DB using Java PreparedStatement.



Solution: It is as simple as

Replace single quote (‘) with UNICODE (\u2019) value and then pass it to the prepared statement with a bind variable.

Sample Code:

String name = ‘O'Connor’;
String newName = name.replaceAll("'", "\u2019");
String insertQuery = “INSERT INTO TABLE_NAME (COLUMN_NAME)VALUES (?)";
try {
                conn = getConnection();
                pstmt = conn.prepareStatement(insertQuery);
                pstmt.setString(1, newName);
                int count = pstmt.executeUpdate();
    }

Friday 21 September 2012

Welcome to my Java Help Blog

Hello Friends,

I would like to keep on posting issues and solutions in Java that we have faced in real time.  I hope you enjoy my blog postings.

Ramesh Kumar