在Tomcat服務器下使用連接池連接Oracle數據庫建站
導讀:1建站知識本文為大家介紹下在Tomcat服務器下使用連接池來連接數據庫的操作,下面有個不錯的示例,大家可以參考下網站優化seo培訓建設網站。
下面介紹在Tomcat服務器下使用連接池來連接數據庫的操作 一:修改web.xml文件:
復制代碼 代碼如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee " version="3.0"> <display-name>project</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <resource-ref> <description>DBConnection</description> <res-ref-name>siniteksirm</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> </web-app>
在web-app之間插入<resource-ref>這段代碼。指定要是用的Resource名稱。 二:修改tomcat下的context.xml文件: 在Context標簽之間加入如下代碼。復制代碼 代碼如下:
<Resource auth="Container" type="javax網站建設制作.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver" url="jdbc:oracle:thin:@192.168.1.196:1521:orcl" username="paxt" password="paxt" maxActive="20" 網站建設哪家好maxIdle="10" maxWait="-1" testOnBorrow="true" validationQuery="select 1 from dual"/>
三:選擇Oracle的數據庫驅動,加入到Tomcat的lib包中。本項目中為:Ojdbc14.jar. 四:提供一個jsp頁面:復制代碼 代碼如下:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="javax.naming.Context" %> <%@ page import="javax.naming.InitialContext" %> <%@ page import="java.sql.*" %> <%@ page import=營銷型網站建設"javax.sql.DataSource" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% DataSource ds = null; try{ Context context = new InitialContext(); ds = (DataSource)context.lookup("java:comp/env/siniteksirm"); Connection conn = ds.getConnection(); PreparedStatement pst = conn.prepareStatement("select * from sdc_fundbase where rownum <= 2"); ResultSet rs = pst.executeQuery(); while(rs.next()){ out.println(rs.getString("fund4")); out.println("<br/>"); } if(ds != null){ out.println("數據庫連接"); } }catch(Exception e){ e.printStackTrace(); out.println("數據庫連接失敗"); } %> </body> </html>
聲明: 本文由我的SEOUC技術文章主頁發布于:2023-05-22 ,文章在Tomcat服務器下使用連接池連接Oracle數據庫建站主要講述標簽,數據庫,服務器網站建設源碼以及服務器配置搭建相關技術文章。轉載請保留鏈接: http://www.bifwcx.com/article/web_5393.html