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

  • 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,执行查询操作。

     
    import java.sql.Connection; 
    import java.sql.DriverManager; 
    import java.sql.ResultSet; 
    import java.sql.Statement; 
    public class PostgreSQLJDBC2 {      
      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 = null;              
          System.out.println("Opened database successfully");              
          sql = "SELECT * FROM COMPANY;";              
          ResultSet res=stmt.executeQuery(sql);              
          while(res.next()) {                  
            System.out.println(res.getInt(1));                  
            System.out.println(res.getString(2));                  
            System.out.println(res.getInt(3));                  
            System.out.println(res.getString(4));                  
            System.out.println(res.getDouble(5));              
          }              
          stmt.close();              
          c.close();          
        }          
        catch(Exception e) {              
          System.err.println( e.getClass().getName()+": "+ e.getMessage() );              
          System.exit(0);          
        }      
      } 
     }


文档是否已解决您的问题?

  已解决   未解决

如您有其它疑问,您也可以与我们技术专家联系探讨。

联系技术专家