pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: dmp <danap@ttc-cmc.net>
To: PostgreSQL JDBC <pgsql-jdbc@postgresql.org>
Subject: Re: Java String saving as unicode  in database
Date: Sat, 12 Oct 2013 10:44:36 -0600
Message-ID: <52597C74.6070104@ttc-cmc.net> (raw)
In-Reply-To: <1381565943021-5774370.post@n5.nabble.com>
References: <1381565943021-5774370.post@n5.nabble.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-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.



-- 
Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-jdbc



view thread (6+ messages)  latest in thread

Message-ID: <52597C74.6070104@ttc-cmc.net>
Permalink:  ../52597C74.6070104@ttc-cmc.net/
Also on:    postgresql.org/message-id/52597C74.6070104@ttc-cmc.net

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-jdbc@postgresql.org
  Cc: danap@ttc-cmc.net
  Subject: Re: Java String saving as unicode  in database
  In-Reply-To: <52597C74.6070104@ttc-cmc.net>
  References: <1381565943021-5774370.post@n5.nabble.com> <52597C74.6070104@ttc-cmc.net>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox