1.选择的驱动版本(jdk1.8+oracle11G)
2.获取驱动到本地
3.将驱动配置到maven
如果配置了环境变量命令操作符执行即可。
未配置环境变量需要在maven的bin目录下
mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc8 -Dversion=12.2.0.1 -Dpackaging=jar -Dfile=C:\Users\Administrator\Desktop\ojdbc8.jar
//-DgroupId=com.oracle maven仓库中的位置,maven仓库为D:\localRepository1,则导入完成后在D:\localRepository1\com\oracle能看到ojdbc7则为成功
//-DartifactId=ojdbc8 -Dversion=12.2.0.1 版本信息
//-Dfile=C:\Users\Administrator\Desktop\ojdbc8.jar 驱动在本地的位置
4.pom文件中引入驱动依赖
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>12.2.0.1</version>
</dependency>
5.jdbc连接oracle数据库
数据库中有如下配置,所有url得修改为
String jdbcUrl = "jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=IP)(PORT=端口))(CONNECT_DATA=(SERVICE_NAME=数据库)))";
String jdbcUrl = "jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=IP)(PORT=端口))(CONNECT_DATA=(SERVICE_NAME=数据库)))";
String username = "用户名";
String password = "密码";
List<String> list=new ArrayList<>();
// SQL 查询
String sql = "SELECT LOGINID FROM test.user" +
"WHERE rmpcode= ? " +
"AND name LIKE ? " +
"AND ENDDATE IS NULL " +
"AND status < ?";
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
// 连接到数据库并执行查询
try (Connection connection = DriverManager.getConnection(jdbcUrl, username, password);
PreparedStatement statement = connection.prepareStatement(sql)) {
// 设置查询参数
statement.setInt(1, 82); // SUBCOMPANYID1
statement.setString(2, "10%"); // LOGINID LIKE '10%'
statement.setInt(3, 5); // status < 5
// 执行查询
try (ResultSet resultSet = statement.executeQuery()) {
while (resultSet.next()) {
// 处理结果
list.add(resultSet.getString("LOGINID"));
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;