1、确保已经安装了Oracle数据库和JDK。

2、下载并安装Oracle JDBC驱动(ojdbc8.jar)。
3、创建一个JSP项目,并将ojdbc8.jar添加到项目的lib目录下。
1、使用SQL*Plus登录到Oracle数据库。
2、创建一个新的表,employees,包含以下字段:id(主键,自增长),name(姓名),age(年龄),salary(薪水)。
CREATE TABLE employees ( id NUMBER(10) PRIMARY KEY, name VARCHAR2(50), age NUMBER(3), salary NUMBER(10, 2) );
1、在JSP页面中引入Oracle JDBC驱动。
<%@ page import="java.sql.*" %> <%@ page import="oracle.jdbc.pool.OracleDataSource" %> <%@ page import="oracle.jdbc.pool.OracleConnectionPoolDataSource" %>
2、配置数据库连接池。
<%
String url = "jdbc:oracle:thin:@localhost:1521:orcl"; // 数据库连接字符串
String user = "用户名"; // 数据库用户名
String password = "密码"; // 数据库密码
int initialSize = 5; // 初始化连接数
int maxActive = 10; // 最大活动连接数
int minIdle = 2; // 最小空闲连接数
int maxWait = 10000; // 获取连接时最大等待时间(毫秒)
int timeBetweenEvictionRunsMillis = 60000; // 连接回收策略的运行间隔时间(毫秒)
int minEvictableIdleTimeMillis = 300000; // 连接空闲时间超过此值,将被回收(毫秒)
int validationQuery = "SELECT 1 FROM DUAL"; // 验证连接是否有效的查询语句
int testWhileIdle = true; // 是否在空闲时检查连接的有效性
boolean testOnBorrow = false; // 是否在获取连接时检查连接的有效性
boolean testOnReturn = false; // 是否在归还连接时检查连接的有效性
boolean poolPreparedStatements = true; // 是否缓存PreparedStatement对象
int maxOpenPreparedStatements = 1; // 打开的PreparedStatement对象的最大数量
%>
<%!
OracleConnectionPoolDataSource dataSource;
%>
<%
dataSource = new OracleConnectionPoolDataSource();
dataSource.setURL(url);
dataSource.setUser(user);
dataSource.setPassword(password);
dataSource.setInitialSize(initialSize);
dataSource.setMaxActive(maxActive);
dataSource.setMinIdle(minIdle);
dataSource.setMaxWait(maxWait);
dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
dataSource.setValidationQuery(validationQuery);
dataSource.setTestWhileIdle(testWhileIdle);
dataSource.setTestOnBorrow(testOnBorrow);
dataSource.setTestOnReturn(testOnReturn);
dataSource.setPoolPreparedStatements(poolPreparedStatements);
dataSource.setMaxOpenPreparedStatements(maxOpenPreparedStatements);
%>
3、编写添加员工信息的函数。
<%!
public void addEmployee(String name, int age, double salary) {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = dataSource.getConnection(); // 从连接池中获取一个连接
String sql = "INSERT INTO employees (name, age, salary) VALUES (?, ?, ?)"; // SQL插入语句
preparedStatement = connection.prepareStatement(sql); // 预编译SQL语句
preparedStatement.setString(1, name); // 设置参数值
preparedStatement.setInt(2, age); // 设置参数值
preparedStatement.setDouble(3, salary); // 设置参数值
preparedStatement.executeUpdate(); // 执行SQL语句,插入数据到数据库中
} catch (SQLException e) {
e.printStackTrace(); // 打印异常信息到控制台,方便调试和排查问题
} finally {
if (preparedStatement != null) { // 如果PreparedStatement对象不为空,关闭它释放资源
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace(); // 打印异常信息到控制台,方便调试和排查问题
}
}
if (connection != null) { // 如果Connection对象不为空,关闭它释放资源,并返回到连接池中供其他线程使用
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace(); // 打印异常信息到控制台,方便调试和排查问题
} finally {