请采用Eclipse、IDEA和VSCode等开发工具(其他开发工具也可,任选一),使用java编程语言完成以下功能:(1)编写connectOracle函数实现连接本机安装的Oracle数据库orcl(其他数据库也可以),测试连接过程(函数源码和测试结果);(2)通过java程序实现对scott.emp表的增加(函数insertEmp)、删除(函数deleteEmp指定部门编号进行删除相关雇员)、修改(函数updateEmp,修改某一编号的雇员信息)、查询(函数getEmp,查询给定部门编号雇员信息)操作,并测试(函数源码和测试结果);(3)采用Java程序编写executeProcedete()函数,实现调用第二题第四小题的ProQueryEmpByDeptno存储过程,并测试(函数源码和测试结果)。
时间: 2024-03-24 18:40:54 浏览: 80
很抱歉,我作为一个AI语言模型,无法为您提供编程环境,但是我可以为您提供以下的代码示例:
1. connectOracle函数实现连接本机安装的Oracle数据库orcl(其他数据库也可以),测试连接过程:
```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class OracleConnectTest {
public static void connectOracle(){
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
String user = "username";
String password = "password";
Connection conn = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
if (conn != null) {
System.out.println("Oracle数据库连接成功!");
} else {
System.out.println("Oracle数据库连接失败!");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
connectOracle();
}
}
```
2. 通过java程序实现对scott.emp表的增加(函数insertEmp)、删除(函数deleteEmp指定部门编号进行删除相关雇员)、修改(函数updateEmp,修改某一编号的雇员信息)、查询(函数getEmp,查询给定部门编号雇员信息)操作,并测试:
```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class OracleCrudTest {
private static Connection conn = null;
private static PreparedStatement pstmt = null;
private static ResultSet rs = null;
//连接Oracle数据库
private static void connectOracle(){
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
String user = "username";
String password = "password";
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
if (conn != null) {
System.out.println("Oracle数据库连接成功!");
} else {
System.out.println("Oracle数据库连接失败!");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
//查询数据
public static void getEmp(int deptno) {
String sql = "SELECT * FROM emp WHERE deptno = ?";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, deptno);
rs = pstmt.executeQuery();
while (rs.next()) {
System.out.println("empno=" + rs.getInt("empno") + ",ename=" + rs.getString("ename")
+ ",job=" + rs.getString("job") + ",mgr=" + rs.getString("mgr") + ",hiredate="
+ rs.getDate("hiredate") + ",sal=" + rs.getDouble("sal") + ",comm=" + rs.getDouble("comm")
+ ",deptno=" + rs.getInt("deptno"));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
//插入数据
public static void insertEmp(int empno, String ename, String job, int mgr, String hiredate, double sal, double comm, int deptno) {
String sql = "INSERT INTO emp(empno, ename, job, mgr, hiredate, sal, comm, deptno) VALUES(?,?,?,?,?,?,?,?)";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, empno);
pstmt.setString(2, ename);
pstmt.setString(3, job);
pstmt.setInt(4, mgr);
pstmt.setString(5, hiredate);
pstmt.setDouble(6, sal);
pstmt.setDouble(7, comm);
pstmt.setInt(8, deptno);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
//删除数据
public static void deleteEmp(int deptno) {
String sql = "DELETE FROM emp WHERE deptno = ?";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, deptno);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
//更新数据
public static void updateEmp(int empno, String ename, String job, int mgr, String hiredate, double sal, double comm, int deptno) {
String sql = "UPDATE emp SET ename = ?, job = ?, mgr = ?, hiredate = ?, sal = ?, comm = ?, deptno = ? WHERE empno = ?";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, ename);
pstmt.setString(2, job);
pstmt.setInt(3, mgr);
pstmt.setString(4, hiredate);
pstmt.setDouble(5, sal);
pstmt.setDouble(6, comm);
pstmt.setInt(7, deptno);
pstmt.setInt(8, empno);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
connectOracle();
//插入数据测试
insertEmp(8001, "JACK", "SALESMAN", 7698, "1981-11-21", 1250.00, 500.00, 30);
//查询数据测试
getEmp(30);
//更新数据测试
updateEmp(8001, "JACKSON", "SALESMAN", 7698, "1981-11-21", 1500.00, 500.00, 30);
//查询数据测试
getEmp(30);
//删除数据测试
deleteEmp(30);
//查询数据测试
getEmp(30);
}
}
```
3. 采用Java程序编写executeProcedete()函数,实现调用第二题第四小题的ProQueryEmpByDeptno存储过程,并测试:
```java
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Types;
public class OracleStoredProcTest {
private static Connection conn = null;
private static CallableStatement cstmt = null;
//连接Oracle数据库
private static void connectOracle(){
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
String user = "username";
String password = "password";
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
if (conn != null) {
System.out.println("Oracle数据库连接成功!");
} else {
System.out.println("Oracle数据库连接失败!");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
//调用存储过程
public static void executeProcedure(int deptno) {
String sql = "{call ProQueryEmpByDeptno(?,?)}";
try {
cstmt = conn.prepareCall(sql);
cstmt.setInt(1, deptno);
cstmt.registerOutParameter(2, Types.VARCHAR);
cstmt.execute();
System.out.println(cstmt.getString(2));
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
connectOracle();
executeProcedure(30);
}
}
```
注意:以上代码只是示例,需要根据自己实际的数据库和存储过程进行修改。同时,代码中的数据库连接信息也需要修改为自己的信息。
阅读全文