Thread: problem with Chinese

problem with Chinese

From
root
Date:
Hi,
I have downloaded binary JDBC Driver from both postgresql.org and
jdbc.postgresql
it's version is jdbc7.1-1.2/jdbc7.1-1.1. It always give a message like this

Exception in thread "main" java.sql.SQLException: ERROR:��Unterminated
quoted string

�� ����at org.postgresql.Connection.ExecSQL(Connection.java:533)
�� ����at org.postgresql.jdbc2.Statement.execute(Statement.java:294)
�� ����at org.postgresql.jdbc2.Statement.executeQuery(Statement.java:59)
�� ����at Test.main(Test.java:20)

I have also tride to rebuild the source code with
./configure --prefix=/opt --enable-locale --enable-multibyte=EUC_CN
--with-CXX -with-java -with-openssl=/usr
1)��for JDBC it can complete compilation, but use JDBC with problem
2)��for libpq--> input.c -->on_exit() :

gcc -O2 -Wall -Wmissing-prototypes -Wmissing-declarations
-I../../../src/interfaces/libpq -I../../../src/include -I/usr/include��-
c -o input.o input.c
input.c: In function `initializeInput':
input.c:157: warning: passing arg 1 of `on_exit' from incompatible
pointer type
input.c:157: too few arguments to function `on_exit'

3) my table customer is very simple : cno char(10),cname varchar(50)
my source code :

import java.sql.*;
import java.io.*;

class Test{
�� ����public static void main(String args[]) throws Exception {
�� ������������System.out.println("input :");
�� ������������BufferedReader in=new BufferedReader(
�� ������������������������������������new InputStreamReader(
�� ��������������������������������������������System.in
�� ����������������������������������������)
�� ����������������������������������);
�� ������������String t1=in.readLine(); //here i can enter some Chinese
string
�� ������������System.out.println("your input are :"+t1);��
�� ������������Class.forName("org.postgresql.Driver");
�� ������������Connection dbconn=DriverManager.getConnection(
�� ��������������������"jdbc:postgresql:redauto","postgres","");
�� ������������Statement st = dbconn.createStatement();
�� ������������String str_sql="SELECT * FROM customer where cname='"+t1+"'";
�� ������������System.out.println(str_sql);
�� ������������ResultSet rs = st.executeQuery(str_sql);
�� ������������while(rs.next()) {
�� ����������������System.out.println(rs.getString(1)+"\t"+rs.getString
(2)+"\n");
�� ������������}
�� ������������rs.close();
�� ������������st.close();
�� ������������dbconn.close();
�� ����}
}



I am shade to have disturbed you.
you are kind for me if you can give any sugestion.
Thanks very much.

malix
shanghai china


Re: problem with Chinese

From
Barry Lind
Date:
The problem here could be a number of different things.  Let me walk
through the ones I can think of.

1) You really should be using a PreparedStatement for this query you are
doing, so that the driver can escape any special characters you may have
in your bind value.  Right now if your t1 value contains single quotes
you will get an error.  So:

> String str_sql="SELECT * FROM customer where cname='"+t1+"'";
> �� ������������System.out.println(str_sql);
> �� ������������ResultSet rs = st.executeQuery(str_sql);

should be:

PreparedStatement st = dbconn.prepareStatement("SELECT * FROM customer
where cname= ? ");
st.setString(1,t1);
ResultSet rs = st.executeQuery();


2)  What characterSet/encoding is your database using?  Issue "select
getdatabaseencoding();" from psql while connected to the redauto
database.  It shouldn't be SQL_ASCII, but should be something like EUC_CN.

3)  Have the server print out the SQL statements that are being sent by
the client (set debug_print_query = true in the postgresql.conf file).
By looking at the sql that is being sent to the server you might be able
to track down where the missing/extra quote is coming from.

thanks,
--Barry




root wrote:

> Hi,
> I have downloaded binary JDBC Driver from both postgresql.org and
> jdbc.postgresql
> it's version is jdbc7.1-1.2/jdbc7.1-1.1. It always give a message like this
>
> Exception in thread "main" java.sql.SQLException: ERROR:��Unterminated
> quoted string
>
> �� ����at org.postgresql.Connection.ExecSQL(Connection.java:533)
> �� ����at org.postgresql.jdbc2.Statement.execute(Statement.java:294)
> �� ����at org.postgresql.jdbc2.Statement.executeQuery(Statement.java:59)
> �� ����at Test.main(Test.java:20)
>
> I have also tride to rebuild the source code with
> ./configure --prefix=/opt --enable-locale --enable-multibyte=EUC_CN
> --with-CXX -with-java -with-openssl=/usr
> 1)��for JDBC it can complete compilation, but use JDBC with problem
> 2)��for libpq--> input.c -->on_exit() :
>
> gcc -O2 -Wall -Wmissing-prototypes -Wmissing-declarations
> -I../../../src/interfaces/libpq -I../../../src/include -I/usr/include��-
> c -o input.o input.c
> input.c: In function `initializeInput':
> input.c:157: warning: passing arg 1 of `on_exit' from incompatible
> pointer type
> input.c:157: too few arguments to function `on_exit'
>
> 3) my table customer is very simple : cno char(10),cname varchar(50)
> my source code :
>
> import java.sql.*;
> import java.io.*;
>
> class Test{
> �� ����public static void main(String args[]) throws Exception {
> �� ������������System.out.println("input :");
> �� ������������BufferedReader in=new BufferedReader(
> �� ������������������������������������new InputStreamReader(
> �� ��������������������������������������������System.in
> �� ����������������������������������������)
> �� ����������������������������������);
> �� ������������String t1=in.readLine(); //here i can enter some Chinese
> string
> �� ������������System.out.println("your input are :"+t1);��
> �� ������������Class.forName("org.postgresql.Driver");
> �� ������������Connection dbconn=DriverManager.getConnection(
> �� ��������������������"jdbc:postgresql:redauto","postgres","");
> �� ������������Statement st = dbconn.createStatement();
> �� ������������String str_sql="SELECT * FROM customer where cname='"+t1+"'";
> �� ������������System.out.println(str_sql);
> �� ������������ResultSet rs = st.executeQuery(str_sql);
> �� ������������while(rs.next()) {
> �� ����������������System.out.println(rs.getString(1)+"\t"+rs.getString
> (2)+"\n");
> �� ������������}
> �� ������������rs.close();
> �� ������������st.close();
> �� ������������dbconn.close();
> �� ����}
> }
>
>
>
> I am shade to have disturbed you.
> you are kind for me if you can give any sugestion.
> Thanks very much.
>
> malix
> shanghai china
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 5: Have you checked our extensive FAQ?
>
> http://www.postgresql.org/users-lounge/docs/faq.html
>
>



Re: problem with Chinese

From
root
Date:
Hi all,
I am sorry , I have got the solution.
I should write it so:
�� ������������Class.forName("org.postgresql.Driver");
�� ������������Properties info=new Properties();
�� ������������info.put("user","postgres");
�� ������������info.put("password","");
�� ������������info.put("charSet","GBK");
�� ������������Connection dbconn=DriverManager.getConnection(
�� ��������������������"jdbc:postgresql:redauto",info);
�� ������������Statement st = dbconn.createStatement();
�� ������������String str_sql="SELECT * FROM customer where cname='"+t1+"'";
�� ������������System.out.println(str_sql);
�� ������������ResultSet rs = st.executeQuery(str_sql);

then all Chinese query critery is in order.

but the make error still harass me. if anyone can help me?

>
>
>
>
> I am shade to have disturbed you.
> you are kind for me if you can give any sugestion.
> Thanks very much.
>
> malix
> shanghai china
>