1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;

import org.postgresql.ds.PGSimpleDataSource;

import oracle.jdbc.pool.OracleDataSource;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

public class DBConnection {

    /**
     * @param args
     * @throws SQLException
     */
    public static void main(String[] args) throws SQLException {
        // TODO Auto-generated method stub
        MysqlDataSource mysqlDataSource = new MysqlDataSource();
        // mysqlDataSource.setPassword("dev");
        // mysqlDataSource.setUser("dev");
        mysqlDataSource
                .setURL("jdbc:mysql://localhost/forJava?user=dev&password=dev");
        Connection conn = mysqlDataSource.getConnection();
        Statement stmt = conn.createStatement();
        stmt.executeUpdate("create table if not exists web\n" + "(\n"
                + "        id int not null primary key,\n" + "        name varchar(100),\n"
                + "        created timestamp,\n" + "        content blob\n" + ");\n" + "");
        for (int i = 0; i < 1; i++) {
            stmt.executeUpdate("insert into web (name,content) values ('HL','frgertrhrtnthtohioh')");
        }
        showResultSet(stmt.executeQuery("select * from web limit 10"));

        // Driver driver = new com.mysql.jdbc.Driver();
        // driver.connect("jdbc:mysql://localhost/forJava?user=dev&password=dev",
        // null);

        PGSimpleDataSource pgSimpleDataSource = new PGSimpleDataSource();
        pgSimpleDataSource.setServerName("localhost");
        pgSimpleDataSource.setDatabaseName("dev");
        pgSimpleDataSource.setUser("dev");
        pgSimpleDataSource.setPassword("dev");
        conn = pgSimpleDataSource.getConnection();
        // conn =
        // DriverManager.getConnection("jdbc:postgresql://localhost/test",
        // "dev", "dev");
        showResultSet(conn.createStatement().executeQuery("select * from cities"));

        OracleDataSource oraDataSource = new OracleDataSource();
        // oraDataSource.setServerName("127.0.0.1");
        // oraDataSource.setDatabaseName("HR");
        // oraDataSource.setUser("HR");
        // oraDataSource.setPassword("HR");
        oraDataSource.setURL("jdbc:oracle:thin:hr/hr@//localhost:1521/XE");
        conn = oraDataSource.getConnection();
        stmt = conn.createStatement();
        stmt.execute("select * from tab");
        showResultSet(stmt.getResultSet());
        stmt.execute("select * from jobs");
        showResultSet(stmt.getResultSet());
        stmt.execute("select * from DEPARTMENTS");
        showResultSet(stmt.getResultSet());
    }

    static void showResultSet(ResultSet resultSet) throws SQLException {
        ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
        int num = resultSetMetaData.getColumnCount();
        while (resultSet.next()) {
            for (int i = 1; i <= num; i++) {
                System.out.print(resultSetMetaData.getCatalogName(i) + " "
                        + resultSet.getString(i));
            }
            System.out.println();
        }
    }
}

呃,在自己电脑上同时安装了mysql, postgresql,oracle,db2,sqlite的人是不是很蛋疼?