Home > 我就是未分类 > JDBC连接DB2、Oracle、MySQL、PostgreSQL,并取当前时间(转)

JDBC连接DB2、Oracle、MySQL、PostgreSQL,并取当前时间(转)

September 29th, 2004

 

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();
}
}
}

我就是未分类

  1. No comments yet.
  1. No trackbacks yet.