JDBC连接DB2、Oracle、MySQL、PostgreSQL
来源:岁月联盟
时间:2008-01-28
JDBC连接DB2、Oracle、MySQL、PostgreSQL,并取得当前时间得实用代码:
package com.terac.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class DBTest { public static void main(String[] args) { try { String user = "user"; String password = "password"; String driver = "COM.ibm.db2.jdbc.net.DB2Driver"; //"oracle.jdbc.driver.OracleDriver"; //"com.mysql.jdbc.Driver"; //"org.postgresql.Driver"; String url = "jdbc:db2://localhost/a"; //"jdbc:oracle:thin:@localhost:1521:a"; //"jdbc:mysql://localhost/a"; //"jdbc:postgresql://localhost:5740/a"; String sql = "SELECT current timestamp FROM sysibm.sysdummy1"; //"SELECT sysdate FROM dual"; //"SELECT now()"; //"SELECT current_timestamp"; Class.forName(driver).newInstance(); Connection c = DriverManager.getConnection(url, user, password); Statement stmt = c.createStatement(); ResultSet rs = stmt.executeQuery(sql); while (rs.next()) { System.out.println(rs.getString(1)); } } catch (Exception e) { e.printStackTrace(); } } } |