所有支持JDBC访问数据库的操作方式都是一样的。
Connection con = DriverManager.getConnection(jdbcUrl, username, password);
st = con.createStatement();
st.setFetchSize(3);
ResultSet rs = st.executeQuery(sql);
区别在于以下几点
Jar
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
驱动加载
Class.forName("com.mysql.cj.jdbc.Driver");
jdbcUrl
jdbc:mysql://192.168.7.10:3306/zzcg?useCursorFetch=true
Jar
<!--连接SQL Server-->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>4.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/libs/sqljdbc4-4.0.jar</systemPath>
</dependency>
驱动加载
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
jdbcUrl
jdbc:sqlserver://192.168.7.101:1433;DatabaseName=zdb;
Jar
<!--连接Oracle-->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/libs/ojdbc6.jar</systemPath>
</dependency>
驱动加载
Class.forName("oracle.jdbc.driver.OracleDriver");
jdbcUrl
jdbc:oracle:thin:@192.168.7.101:1521:orcl
Jar
<!--Hive JDBC-->
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>2.1.0</version>
<exclusions>
<exclusion>
<artifactId>protobuf-java</artifactId>
<groupId>com.google.protobuf</groupId>
</exclusion>
<exclusion>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
驱动加载
Class.forName("org.apache.hive.jdbc.HiveDriver");
jdbcUrl
jdbc:hive2://192.168.7.101:10000/default
Jar
<!--连接PostgreSQL-->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.2</version>
</dependency>
驱动加载
Class.forName("org.postgresql.Driver");
jdbcUrl
jdbc:postgresql://localhost:5432/db_person