Re: Java String saving as unicode in database

From: dmp <danap(at)ttc-cmc(dot)net>
To: PostgreSQL JDBC <pgsql-jdbc(at)postgresql(dot)org>
Subject: Re: Java String saving as unicode in database
Date: 2013-10-12 16:44:36
Message-ID: 52597C74.6070104@ttc-cmc.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

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.

In response to

Responses

Browse pgsql-jdbc by date

  From Date Subject
Next Message dmp 2013-10-12 16:52:38 Re: Saving spanish text into DB (as unicode)
Previous Message saisantoshi 2013-10-12 08:23:24 Saving spanish text into DB (as unicode)