代码之家  ›  专栏  ›  技术社区  ›  Anders Lindahl

从Linux(ubuntu)机器访问iSeries上DB2数据库的最简单方法?

  •  0
  • Anders Lindahl  · 技术社区  · 17 年前

    IBM建议使用的ODBC驱动程序 System i Access for Linux unixODBC ,但由于这两种系统对我来说都是新的,我想知道是否还有其他方法可以做到这一点。

    涉及 telnet 和 expect

    3 回复  |  直到 17 年前
        1
  •  2
  •   Allen    16 年前

    我想这是最简单的。。。
    与Access for Linux相同,但它是开源版本: JTOpen

    iSeries Information Center :

     // Connect to the server.
     Connection c = DriverManager.getConnection("jdbc:as400://mySystem");
    
     // Create a Statement object.
     Statement s = c.createStatement();
    
     // Run an SQL statement that creates
     // a table in the database.
     s.executeUpdate("CREATE TABLE MYLIBRARY.MYTABLE (NAME VARCHAR(20), ID INTEGER)");
    
     // Run an SQL statement that inserts
     // a record into the table.
     s.executeUpdate("INSERT INTO MYLIBRARY.MYTABLE (NAME, ID) VALUES ('DAVE', 123)");
    
     // Run an SQL statement that inserts
     // a record into the table.
     s.executeUpdate("INSERT INTO MYLIBRARY.MYTABLE (NAME, ID) VALUES ('CINDY', 456)");
    
     // Run an SQL query on the table.
     ResultSet rs = s.executeQuery("SELECT * FROM MYLIBRARY.MYTABLE");
    
     // Close the Statement and the
     // Connection.
     s.close();
     c.close();
    
        2
  •  0
  •   ppuschmann    17 年前


    DB2 Checker
    Download DB2 Checker

    未经测试的。。。

        3
  •  0
  •   robertnl    16 年前

    我的磁盘上有这个来源。这是一个很好的例子。为了清楚起见,我在源代码中更改了机器、目录和表名。希望能有帮助。它使用JTOpen项目中的JDBC驱动程序。

    注意,使用这个特定的驱动程序,您可以像访问任何其他数据库一样访问iSeries DB2数据库。此外,iSeries上的DB2数据库只是IBM Universal数据库的一个版本。但是,即使使用JDBC驱动程序,您也可以使用iSeries特定的技巧。但如果您只想保留SQL默认值,也可以。

    import java.sql.*;
    
    public class TestSQL {
    
     public static void main(String[] args) {
    
      try {
       DriverManager
         .registerDriver(new com.ibm.as400.access.AS400JDBCDriver());
       Connection conn = DriverManager
         .getConnection("jdbc:as400://YOURISERIES/YOURLIBRARY");
       Statement select = conn.createStatement();
       ResultSet rs = select.executeQuery("select * from whatever");
    
       ResultSetMetaData rsmd = rs.getMetaData(); 
       while (rs.next()) {
        for (int x=1; x <= rsmd.getColumnCount(); x++) {
         System.out.print(rs.getString(x));
        }
        System.out.println();
       }
      } catch (Exception e) {
       System.out.println("error: " + e.getMessage());
      }
      System.exit(0);
     }
    }