pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Java String saving as unicode  in database
6+ messages / 3 participants
[nested] [flat]

* Java String saving as unicode  in database
@ 2013-10-12 08:19 saisantoshi <mr@starsborn.com>
  2013-10-12 16:44 ` Re: Java String saving as unicode  in database dmp <danap@ttc-cmc.net>
  0 siblings, 1 reply; 6+ messages in thread

From: saisantoshi @ 2013-10-12 08:19 UTC (permalink / raw)
  To: pgsql-jdbc

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



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Java String saving as unicode  in database
  2013-10-12 08:19 Java String saving as unicode  in database saisantoshi <mr@starsborn.com>
@ 2013-10-12 16:44 ` dmp <danap@ttc-cmc.net>
  2013-10-12 23:55   ` Re: Java String saving as unicode  in database saisantoshi <mr@starsborn.com>
  0 siblings, 1 reply; 6+ messages in thread

From: dmp @ 2013-10-12 16:44 UTC (permalink / raw)
  To: 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.



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



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Java String saving as unicode  in database
  2013-10-12 08:19 Java String saving as unicode  in database saisantoshi <mr@starsborn.com>
  2013-10-12 16:44 ` Re: Java String saving as unicode  in database dmp <danap@ttc-cmc.net>
@ 2013-10-12 23:55   ` saisantoshi <mr@starsborn.com>
  2013-10-13 00:38     ` Re: Java String saving as unicode  in database John R Pierce <pierce@hogranch.com>
  2013-10-13 16:46     ` Re: Java String saving as unicode  in database dmp <danap@ttc-cmc.net>
  0 siblings, 2 replies; 6+ messages in thread

From: saisantoshi @ 2013-10-12 23:55 UTC (permalink / raw)
  To: pgsql-jdbc

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....
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



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Java String saving as unicode  in database
  2013-10-12 08:19 Java String saving as unicode  in database saisantoshi <mr@starsborn.com>
  2013-10-12 16:44 ` Re: Java String saving as unicode  in database dmp <danap@ttc-cmc.net>
  2013-10-12 23:55   ` Re: Java String saving as unicode  in database saisantoshi <mr@starsborn.com>
@ 2013-10-13 00:38     ` John R Pierce <pierce@hogranch.com>
  1 sibling, 0 replies; 6+ messages in thread

From: John R Pierce @ 2013-10-13 00:38 UTC (permalink / raw)
  To: pgsql-jdbc

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



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



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Java String saving as unicode  in database
  2013-10-12 08:19 Java String saving as unicode  in database saisantoshi <mr@starsborn.com>
  2013-10-12 16:44 ` Re: Java String saving as unicode  in database dmp <danap@ttc-cmc.net>
  2013-10-12 23:55   ` Re: Java String saving as unicode  in database saisantoshi <mr@starsborn.com>
@ 2013-10-13 16:46     ` dmp <danap@ttc-cmc.net>
  2013-10-13 20:06       ` Re: Java String saving as unicode  in database dmp <danap@ttc-cmc.net>
  1 sibling, 1 reply; 6+ messages in thread

From: dmp @ 2013-10-13 16:46 UTC (permalink / raw)
  To: pgsql-jdbc

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....
> 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



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Java String saving as unicode  in database
  2013-10-12 08:19 Java String saving as unicode  in database saisantoshi <mr@starsborn.com>
  2013-10-12 16:44 ` Re: Java String saving as unicode  in database dmp <danap@ttc-cmc.net>
  2013-10-12 23:55   ` Re: Java String saving as unicode  in database saisantoshi <mr@starsborn.com>
  2013-10-13 16:46     ` Re: Java String saving as unicode  in database dmp <danap@ttc-cmc.net>
@ 2013-10-13 20:06       ` dmp <danap@ttc-cmc.net>
  0 siblings, 0 replies; 6+ messages in thread

From: dmp @ 2013-10-13 20:06 UTC (permalink / raw)
  To: pgsql-jdbc

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.


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



^ permalink  raw  reply  [nested|flat] 6+ messages in thread


end of thread, other threads:[~2013-10-13 20:06 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2013-10-12 08:19 Java String saving as unicode  in database saisantoshi <mr@starsborn.com>
2013-10-12 16:44 ` dmp <danap@ttc-cmc.net>
2013-10-12 23:55   ` saisantoshi <mr@starsborn.com>
2013-10-13 00:38     ` John R Pierce <pierce@hogranch.com>
2013-10-13 16:46     ` dmp <danap@ttc-cmc.net>
2013-10-13 20:06       ` dmp <danap@ttc-cmc.net>

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