zoukankan      html  css  js  c++  java
  • jdbc使用DataSource连接mysql,postgresql,oracle的代码

    jdbc连接数据库,使用DataSource是推荐的方式

    (jdbc驱动是当然要放进classpath里的,官网一般都有下载)

     1 import java.sql.Connection;
     2 import java.sql.ResultSet;
     3 import java.sql.ResultSetMetaData;
     4 import java.sql.SQLException;
     5 import java.sql.Statement;
     6 
     7 import org.postgresql.ds.PGSimpleDataSource;
     8 
     9 import oracle.jdbc.pool.OracleDataSource;
    10 
    11 import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
    12 
    13 public class DBConnection {
    14 
    15     /**
    16      * @param args
    17      * @throws SQLException
    18      */
    19     public static void main(String[] args) throws SQLException {
    20         // TODO Auto-generated method stub
    21         MysqlDataSource mysqlDataSource = new MysqlDataSource();
    22         // mysqlDataSource.setPassword("dev");
    23         // mysqlDataSource.setUser("dev");
    24         mysqlDataSource
    25                 .setURL("jdbc:mysql://localhost/forJava?user=dev&password=dev");
    26         Connection conn = mysqlDataSource.getConnection();
    27         Statement stmt = conn.createStatement();
    28         stmt.executeUpdate("create table if not exists web\n" + "(\n"
    29                 + "        id int not null primary key,\n" + "        name varchar(100),\n"
    30                 + "        created timestamp,\n" + "        content blob\n" + ");\n" + "");
    31         for (int i = 0; i < 1; i++) {
    32             stmt.executeUpdate("insert into web (name,content) values ('HL','frgertrhrtnthtohioh')");
    33         }
    34         showResultSet(stmt.executeQuery("select * from web limit 10"));
    35 
    36         // Driver driver = new com.mysql.jdbc.Driver();
    37         // driver.connect("jdbc:mysql://localhost/forJava?user=dev&password=dev",
    38         // null);
    39 
    40         PGSimpleDataSource pgSimpleDataSource = new PGSimpleDataSource();
    41         pgSimpleDataSource.setServerName("localhost");
    42         pgSimpleDataSource.setDatabaseName("dev");
    43         pgSimpleDataSource.setUser("dev");
    44         pgSimpleDataSource.setPassword("dev");
    45         conn = pgSimpleDataSource.getConnection();
    46         // conn =
    47         // DriverManager.getConnection("jdbc:postgresql://localhost/test",
    48         // "dev", "dev");
    49         showResultSet(conn.createStatement().executeQuery("select * from cities"));
    50 
    51         OracleDataSource oraDataSource = new OracleDataSource();
    52         // oraDataSource.setServerName("127.0.0.1");
    53         // oraDataSource.setDatabaseName("HR");
    54         // oraDataSource.setUser("HR");
    55         // oraDataSource.setPassword("HR");
    56         oraDataSource.setURL("jdbc:oracle:thin:hr/hr@//localhost:1521/XE");
    57         conn = oraDataSource.getConnection();
    58         stmt = conn.createStatement();
    59         stmt.execute("select * from tab");
    60         showResultSet(stmt.getResultSet());
    61         stmt.execute("select * from jobs");
    62         showResultSet(stmt.getResultSet());
    63         stmt.execute("select * from DEPARTMENTS");
    64         showResultSet(stmt.getResultSet());
    65     }
    66 
    67     static void showResultSet(ResultSet resultSet) throws SQLException {
    68         ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
    69         int num = resultSetMetaData.getColumnCount();
    70         while (resultSet.next()) {
    71             for (int i = 1; i <= num; i++) {
    72                 System.out.print(resultSetMetaData.getCatalogName(i) + " "
    73                         + resultSet.getString(i));
    74             }
    75             System.out.println();
    76         }
    77     }
    78 }




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

  • 相关阅读:
    php : Warning: strftime(): It is not safe to rely on the system's timezone settings.
    php : DOM 操作 XML
    php : 基础(3)
    php : 基础(2)
    阿里云物联网平台体验(NetGadgeteer+C#篇)
    阿里云物联网平台体验(树莓派+Nodejs篇)
    阿里云物联网平台体验(树莓派+Python篇)
    【MVP时间】5节课助你破解物联网硬件接入难点
    破解物联网落地困境-阿里云硬件接入最佳实践
    从端到云——工业物联网项目全栈快速开发实践
  • 原文地址:https://www.cnblogs.com/windydays/p/2298101.html
Copyright © 2011-2022 走看看