免费注册
帮助文档(华北一、二)

  • Linux操作系统

     yum install postgresql-jdbc.noarch –y

    Windows环境下JDBC驱动,将jar添加到工程的BUILD PATH。

    示例程序1,java连接DW,执行建表,插入操作。

    ● PostgreSQLJDBC1.java

     
    import java.sql.Connection; 
    import java.sql.DriverManager; 
    import java.sql.Statement; 
    public class PostgreSQLJDBC1 {     
      public static void main(String args[]) {         
        Connection c = null;         
        Statement stmt = null;         
        try {             
          Class.forName("org.postgresql.Driver");             
          c = 
    DriverManager.getConnection("jdbc:postgresql://hostIP:port/dbname",”UserName”,”Password”);             
          stmt = c.createStatement();             
          String sql = "CREATE TABLE COMPANY " + "(ID INT PRIMARY KEY NOT NULL," + "NAME TEXT NOT NULL," + "AGE INT NOT NULL," + "ADDRESS CHAR(50)," + "SALARY REAL)";         
        c.setAutoCommit(false);         
        System.out.println("Opened database successfully");         
        stmt.executeUpdate(sql);         
        sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) " + "VALUES (1, 'Allen', 25 , 'Texas', 15000.00 );";          
        stmt.executeUpdate(sql);          
        stmt.close();         
        c.commit();         
        c.close();         
        }           
        catch (Exception e) {             
          e.printStackTrace();             
          System.err.println(e.getClass().getName()+": "+e.getMessage());             
          System.exit(0);         
        }         
        System.out.println("Opened database successfully");     
      } 
    }

    示例程序二:java连接DW,执行查询操作。