public class jdbcTest {
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@192.168.238.130:1521:orcl";
String username = "scott";
String password = "tiger";
/**
*
* jdbc连接Oracle数据库,并对emp表进行查询操作
*/
@Test
public void findAll() throws ClassNotFoundException, SQLException {
//1.加载驱动
Class.forName(driver);
//2.获取连接
Connection connection = DriverManager.getConnection(url, username, password);
//3.执行SQL语句,获取预编译对象
PreparedStatement preparedStatement = connection.prepareStatement("select * from emp");
//4.执行查询
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()){
System.out.println(resultSet.getInt(1)+" "+resultSet.getString("ename"));
}
//5.释放资源
resultSet.close();
preparedStatement.close();
connection.close();
}
/**
*
* jdbc连接oracle数据库并调用存储过程,(需要在控制台输出结果)
*
*/
@Test
public void callProcedure2() throws ClassNotFoundException, SQLException {
//1.加载驱动
Class.forName(driver);
//2.获取连接
Connection connection = DriverManager.getConnection(url, username, password);
//3.调用存储过程,获取调用语句
CallableStatement callableStatement = connection.prepareCall("{call pro_total_sal(?,?)}");
//4.设置参数1,注册参数2
callableStatement.setInt(1,7499);
callableStatement.registerOutParameter(2,OracleTypes.INTEGER);
//5.执行查询,
callableStatement.execute();
System.out.println(callableStatement.getInt(2));
//5.释放资源
callableStatement.close();
connection.close();
}
}
IDEA连接Oracle数据库并调用存储过程
最新推荐文章于 2025-04-26 12:36:13 发布