1、导入依赖
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>12.2.0.1</version>
</dependency>
2、创建jdbc连接实现查询
public class Test01 {
public static void main(String[] args) throws Exception {
//加载数据库驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//得到Connection连接
Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:ORCL",
"c##boat", "c##boat");
//得到预编译的Statement对象
PreparedStatement pstm = connection.prepareStatement("select * from student where id = ?");
//给参数赋值
pstm.setObject(1, 1);
//执行数据库查询操作
ResultSet rs = pstm.executeQuery();
//输出结果
while(rs.next()){
System.out.println(rs.getString("name"));
}
//释放资源
rs.close();
pstm.close();
connection.close();
}
}
3、jdbc调用存储函数
//加载数据库驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//得到Connection连接
Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:ORCL","c##boat", "c##boat");
//得到预编译的Statement对象,调用存储函数f_yearsal( ? )
PreparedStatement pstm = connection.prepareStatement("select f_yearsal( ? ) NAME from dual");
//给参数赋值
pstm.setObject(1, 2);
//执行数据库查询操作
ResultSet rs = pstm.executeQuery();
//输出结果
while(rs.next()){
System.out.println(rs.getString("NAME"));
}
//释放资源
rs.close();
pstm.close();
connection.close();