av激情亚洲男人的天堂国语,日韩欧美精品一中文字幕,无码av一区二区三区无码,国产又色又爽又刺激的a片,国产又色又爽又刺激的a片

輕松操作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ù)庫
網(wǎng)頁地址:http://uogjgqi.cn/article/dhdoddi.html
掃二維碼與項(xiàng)目經(jīng)理溝通

我們?cè)谖⑿派?4小時(shí)期待你的聲音

解答本文疑問/技術(shù)咨詢/運(yùn)營(yíng)咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流