Обсуждение: Java String saving as unicode in database
Hi, I want to store java string as a unicode string in the database. Please let me know if this is possible? For example : String columnValue="*Sample \u0020\u0061\u0074\u0020\u0032\u0032\u0042\u0020 text*"; I want to save the above exactly (as it is which is bolded) into the database and retreive it. DB column should store as : ColumnA --------- Sample \u0020\u0061\u0074\u0020\u0032\u0032\u0042\u0020 text ( with unicode string value) When read it back from java, it should display: Sample at 22B text -- View this message in context: http://postgresql.1045698.n5.nabble.com/Java-String-saving-as-unicode-in-database-tp5774370.html Sent from the PostgreSQL - jdbc mailing list archive at Nabble.com.
Hello, Example: private void testInsertUTF(Connection con) { // Method Instances String sqlStatementString; Statement sqlStatement; PreparedStatement pstmt; ResultSet rs; try { // Setup a connection statement. sqlStatement = con.createStatement(); // Create table. sqlStatementString = "DROP TABLE IF EXISTS jdbc_demo"; System.out.println(sqlStatementString); sqlStatement.execute(sqlStatementString); sqlStatementString = "Create Table jdbc_demo (col VARCHAR(30))"; System.out.println(sqlStatementString); sqlStatement.execute(sqlStatementString); // Insert data. System.out.println("Inserting Data"); pstmt = con.prepareStatement("INSERT INTO jdbc_demo VALUES (?)"); pstmt.setString(1, "*Sample\u0020\u0061\u0074\u0020\u0032\u0032\u0042\u0020 text*"); pstmt.execute(); // View data. sqlStatementString = "SELECT * FROM jdbc_demo"; System.out.println(sqlStatementString); sqlStatement.execute(sqlStatementString); rs = sqlStatement.executeQuery(sqlStatementString); JPanel panel = new JPanel(); while (rs.next()) { String dataString = rs.getString("col"); System.out.println("col:" + dataString); panel.add(new JLabel(dataString)); } rs.close(); JFrame frame = new JFrame(); frame.getContentPane().add(panel); frame.setSize(200, 200); frame.setVisible(true); // Clean up. sqlStatementString = "DROP TABLE IF EXISTS jdbc_demo"; System.out.println(sqlStatementString); sqlStatement.execute(sqlStatementString); sqlStatement.close(); pstmt.close(); } catch (SQLException sqle) { System.out.println("SQL Exeception" + sqle); } } saisantoshi wrote: > Hi, > > I want to store java string as a unicode string in the database. Please let > me know if this is possible? > > For example : > > String columnValue="*Sample > \u0020\u0061\u0074\u0020\u0032\u0032\u0042\u0020 text*"; > > I want to save the above exactly (as it is which is bolded) into the > database and retreive it. > > DB column should store as : > > ColumnA > --------- > Sample \u0020\u0061\u0074\u0020\u0032\u0032\u0042\u0020 text ( with > unicode string value) > > > When read it back from java, it should display: > Sample at 22B text > > -- > View this message in context: http://postgresql.1045698.n5.nabble.com/Java-String-saving-as-unicode-in-database-tp5774370.html > Sent from the PostgreSQL - jdbc mailing list archive at Nabble.com.
I tried this but the problem is its not saving in the DB as what I wanted to store.. *Sample\u0020\u0061\u0074\u0020\u0032\u0032\u0042\u0020 text Its converting the above and storing it in the DB. When I look at the database, it is something that I can't understand looking at it. May be its an editor problem. How do I view the exact string stored from my windows cmd prompt connecting to postgres sql. Thanks, Sai -- View this message in context: http://postgresql.1045698.n5.nabble.com/Java-String-saving-as-unicode-in-database-tp5774370p5774410.html Sent from the PostgreSQL - jdbc mailing list archive at Nabble.com.
On 10/12/2013 4:55 PM, saisantoshi wrote: > I tried this but the problem is its not saving in the DB as what I wanted to > store.. > *Sample\u0020\u0061\u0074\u0020\u0032\u0032\u0042\u0020 text > > Its converting the above and storing it in the DB. When I look at the > database, it is something that I can't understand looking at it. May be its > an editor problem. How do I view the exact string stored from my windows cmd > prompt connecting to postgres sql. Java (and microsoft windows) uses UCS16, while SQL (and unix/linux and the internet and about everything else) uses UTF8 (but only if its so configured). with SQL, you have two things, the database encoding, and the client_encoding, the latter can be changed on the fly, while the former is setup when the database is initialized and created. -- john r pierce 37N 122W somewhere on the middle of the left coast
As John said. Download and use the complete code from the example given before. Fill in in main() database, username, & password. Compile: javac Test_UTC.java Run: java Test_UTC Look at the cmd line output text and the JPanel text. This will tell the story. Test_UTC code. http://dandymadeproductions.com/temp/Test_UTC.java danap. saisantoshi wrote: > I tried this but the problem is its not saving in the DB as what I wanted to > store.. > *Sample\u0020\u0061\u0074\u0020\u0032\u0032\u0042\u0020 text > > Its converting the above and storing it in the DB. When I look at the > database, it is something that I can't understand looking at it. May be its > an editor problem. How do I view the exact string stored from my windows cmd > prompt connecting to postgres sql. > > Thanks, > Sai > > > > -- > View this message in context: http://postgresql.1045698.n5.nabble.com/Java-String-saving-as-unicode-in-database-tp5774370p5774410.html > Sent from the PostgreSQL - jdbc mailing list archive at Nabble.com. >
I have cleaned up the Test_UTC.java class and renamed it to PostgreSQL_JDBC_Test. Corrected some stuff. This class can be used as a generic test bed for PostgreSQL JDBC connections. Just add a new method for your test case. I will leave this one in this location and update if needed. **REPLACED -->> Test_UTC.java WITH PostgreSQL_JDBC_Test.java http://dandymadeproductions.com/temp/PostgreSQL_JDBC_Test.java danap. dmp wrote: > As John said. Download and use the complete code from the > example given before. Fill in in main() database, username, & > password. > > Compile: > javac Test_UTC.java > > Run: > java Test_UTC > > Look at the cmd line output text and the JPanel text. This will > tell the story. > > Test_UTC code. > http://dandymadeproductions.com/temp/Test_UTC.java > > danap.