当前位置:Linux教程 - Linux - postgresql-chinese-howto

postgresql-chinese-howto



         PostgreSQL与MySQL是Open Source DBMS的两大主力,但我个人比较偏好PostgreSQL.因为她的历史渊源流长,功能强大的多.
    为支持中文,必须重新编译.
    1)下载原代码并解压缩到/usr/src/postgresql-7.1.3/
    2)./configure --prefix=/opt/pgsql --with-multibyte --with-java
    注意一定不能使用 --with-locale 这是为其它非英语地区设致的. 中文和日文一定不能使用这个标记.
    3)make; make install
    4)adduser postgres; mkdir /opt/pgsql/data ; chown postgres /opt/pgsql/data
    5)su postgres -c "/opt/pgsql/bin/initdb -D /opt/pgsql/data"
    6)su postgres -c "/opt/pgsql/bin/postmaster -i -D /opt/pgsql/data"
    7)su postgres -c "/opt/pgsql/bin/createdb test"
    8)/opt/pgsql/bin/psql -U postgres test
    9)测试
    create table test (tno char(10) primary key, tname varchar(100) );
    insert into test values(1000,上海);
    insert into test values(2000,南京);
    select * from test order by tname;
    10)使用JDBC: Test.java
    import java.sql.*;
    import java.io.*;
    import java.util.*;
    class Test{
    public static void main(String args[]) throws Exception {
    System.out.println("输入 TNO:");
    BufferedReader in=new BufferedReader(
    new InputStreamReader(System.in)
    );
    String t1=in.readLine();
    System.out.println("输入 TNAME:");
    String t2=in.readLine();
    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:test",info);
    PreparedStatement st = dbconn.prepareStatement("insert into test values(?,?)");
    st.setString(1,t1);
    st.setString(2,t2);
    st.executeUpdate();

    String str_sql="SELECT * FROM test";
    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();
    }
    }
    11)在Servlet中使用中文访问数据库
    response.setContentType("text/html; charset=GBK");

    //在相应的html文件中有名为firstname 和 lastname 的文本框
    String firstName1 = request.getParameter("firstname");
    String lastName1 = request.getParameter("lastname");

    String firstName,lastName;

    if (firstName1!=null){
    firstName = new String(firstName1.getBytes("8859_1"),"GBK");
    }else{
    firstName = null;
    }
    if (lastName1!=null) {
    lastName = new String(lastName1.getBytes("8859_1"),"GBK");
    }else{
    lastName = null;
    }

    //下面可以使用firstname lastname 作为查询条件访问数据库了.
    发布人:malix 来自: