新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
輕松操作JSP添加Oracle數(shù)據(jù)庫
通過JSP連接Oracle數(shù)據(jù)庫,只需編寫簡(jiǎn)單的代碼,無需繁瑣的配置。輕松實(shí)現(xiàn)數(shù)據(jù)的增刪改查操作。
準(zhǔn)備工作
1、確保已經(jīng)安裝了Oracle數(shù)據(jù)庫和JDK。

2、下載并安裝Oracle JDBC驅(qū)動(dòng)(ojdbc8.jar)。
3、創(chuàng)建一個(gè)JSP項(xiàng)目,并將ojdbc8.jar添加到項(xiàng)目的lib目錄下。
創(chuàng)建數(shù)據(jù)庫表
1、使用SQL*Plus登錄到Oracle數(shù)據(jù)庫。
2、創(chuàng)建一個(gè)新的表,employees,包含以下字段:id(主鍵,自增長(zhǎng)),name(姓名),age(年齡),salary(薪水)。
CREATE TABLE employees ( id NUMBER(10) PRIMARY KEY, name VARCHAR2(50), age NUMBER(3), salary NUMBER(10, 2) );
編寫JSP代碼
1、在JSP頁面中引入Oracle JDBC驅(qū)動(dòng)。
<%@ page import="java.sql.*" %> <%@ page import="oracle.jdbc.pool.OracleDataSource" %> <%@ page import="oracle.jdbc.pool.OracleConnectionPoolDataSource" %>
2、配置數(shù)據(jù)庫連接池。
<%
String url = "jdbc:oracle:thin:@localhost:1521:orcl"; // 數(shù)據(jù)庫連接字符串
String user = "用戶名"; // 數(shù)據(jù)庫用戶名
String password = "密碼"; // 數(shù)據(jù)庫密碼
int initialSize = 5; // 初始化連接數(shù)
int maxActive = 10; // 最大活動(dòng)連接數(shù)
int minIdle = 2; // 最小空閑連接數(shù)
int maxWait = 10000; // 獲取連接時(shí)最大等待時(shí)間(毫秒)
int timeBetweenEvictionRunsMillis = 60000; // 連接回收策略的運(yùn)行間隔時(shí)間(毫秒)
int minEvictableIdleTimeMillis = 300000; // 連接空閑時(shí)間超過此值,將被回收(毫秒)
int validationQuery = "SELECT 1 FROM DUAL"; // 驗(yàn)證連接是否有效的查詢語句
int testWhileIdle = true; // 是否在空閑時(shí)檢查連接的有效性
boolean testOnBorrow = false; // 是否在獲取連接時(shí)檢查連接的有效性
boolean testOnReturn = false; // 是否在歸還連接時(shí)檢查連接的有效性
boolean poolPreparedStatements = true; // 是否緩存PreparedStatement對(duì)象
int maxOpenPreparedStatements = 1; // 打開的PreparedStatement對(duì)象的最大數(shù)量
%>
<%!
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、編寫添加員工信息的函數(shù)。
<%!
public void addEmployee(String name, int age, double salary) {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = dataSource.getConnection(); // 從連接池中獲取一個(gè)連接
String sql = "INSERT INTO employees (name, age, salary) VALUES (?, ?, ?)"; // SQL插入語句
preparedStatement = connection.prepareStatement(sql); // 預(yù)編譯SQL語句
preparedStatement.setString(1, name); // 設(shè)置參數(shù)值
preparedStatement.setInt(2, age); // 設(shè)置參數(shù)值
preparedStatement.setDouble(3, salary); // 設(shè)置參數(shù)值
preparedStatement.executeUpdate(); // 執(zhí)行SQL語句,插入數(shù)據(jù)到數(shù)據(jù)庫中
} catch (SQLException e) {
e.printStackTrace(); // 打印異常信息到控制臺(tái),方便調(diào)試和排查問題
} finally {
if (preparedStatement != null) { // 如果PreparedStatement對(duì)象不為空,關(guān)閉它釋放資源
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace(); // 打印異常信息到控制臺(tái),方便調(diào)試和排查問題
}
}
if (connection != null) { // 如果Connection對(duì)象不為空,關(guān)閉它釋放資源,并返回到連接池中供其他線程使用
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace(); // 打印異常信息到控制臺(tái),方便調(diào)試和排查問題
} finally {
分享名稱:輕松操作JSP添加Oracle數(shù)據(jù)庫
文章鏈接:http://m.5511xx.com/article/dhdoddi.html


咨詢
建站咨詢
