Oracle 专业认证 JavaSE8 编程测验
以下是针对题目中给出的代码的解决方案,这个解决方案是针对如何在Oracle数据库中使用Java代码来处理数据并进行测验的情况。
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class OracleJavaTest {
public static void main(String[] args) {
// 连接数据库
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
// 加载Oracle JDBC驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
// 建立连接
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe", "username", "password");
// 创建SQL语句
String sql = "SELECT * FROM employees";
// 执行查询
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
// 处理结果集
List<Employee> employees = new ArrayList<>();
while (rs.next()) {
Employee emp = new Employee();
emp.setId(rs.getInt("id"));
emp.setName(rs.getString("name"));
emp.setSalary(rs.getDouble("salary"));
employees.add(emp);
}
// 打印结果
for (Employee emp : employees) {
System.out.println("ID: " + emp.getId() + ", Name: " + emp.getName() + ", Salary: " + emp.getSalary());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭资源
try {
if (rs != null) {
rs.close();
rs = null;
}
if (pstmt != null) {
pstmt.close();
pstmt = null;
}
if (conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
static class Employee {
private int id;
private String name;
private double salary;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
评论已关闭