從jdk安裝到jsp連接oracle數據庫的相關配置

1.安裝jdk(版本6u7);

  2.配置jdk環境變量(安裝目錄:D:/tools/java/jdk1.6.0_07):

  1). JAVA_HOME = D:/tools/java/jdk1.6.0_07;

  2). Path的最前面追加"D:/tools/java/jdk1.6.0_07/bin;D:/tools/java/jre1.6.0_07/bin";

  3). CLASSPATH = D:/tools/java/jdk1.6.0_07/lib;D:/tools/java/jdk1.6.0_07/lib/tools.jar;

  3.安裝 tomcat(版本6.0.20)

  4.配置tomcat環境變量(安裝目錄:D:/tools/tomcat6.0)

  1).CATALINA_HOME=D:/tools/tomcat6.0;

  2).CATALINA_BASE=D:/tools/tomcat6.0;

  3).CLASSPATH後面追加"%CATALINA_HOME%/lib/servlet-api.jar;"

  5.安裝oracle數據庫(版本10g)

  數據庫名:orcl

  用戶名:mydata

  密碼:oracle

  6.配置tomcat與oracle的連接

  1).D:/tools/tomcat6.0/conf/server.xml追加如下內容:

  <Context path="/test" reloadable="true" docBase="D:/tools/tomcat6.0/webapps/test" debug="0" >

  <Resource name="jdbc/oracle" auth="Container" type="javax.sql.DataSource"

  maxActive="5" maxIdle="4" maxWait="10000"

  username="mydata" password="oracle" driverClassName="oracle.jdbc.driver.OracleDriver"

  url="jdbc:oracle:thin:@127.0.0.1:1521:ORCL"/>

  </Context>

  說明:①、path="/test"(/test爲tomcat中webapps目錄下的工程文件夾)

  ②、docBase="D:/tools/tomcat6.0/webapps/test"(path中工程文件夾所在的絕對路徑)

  ③、username="mydata"(oracle數據庫的用戶名)

  ④、password="oracle"(oracle數據庫的密碼)

  ⑤、url="jdbc:oracle:thin:@127.0.0.1:1521:ORCL"/>(ORCL爲oracle數據庫名)

  2).

  在D:/tools/tomcat6.0/lib文件夾裏放入oracle驅動文件ojdbc14.jar(此文件可在網上下載)

  3).在webapps下新建工程文件夾test,在test文件夾下新建WEB-INF文件夾和test.jsp文件;

  在WEB-INF文件夾下新建lib文件夾和web.xml文件;在lib文件夾下放入oracle驅動文件

  ojdbc14.jar(此文件可在網上下載)

 

web.xml文件內容如下:

  <?xml version="1.0" encoding="ISO-8859-1"?>

  <!--

  Copyright 2004 The Apache Software Foundation

  Licensed under the Apache License, Version 2.0 (the "License");

  you may not use this file except in compliance with the License.

  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software

  distributed under the License is distributed on an "AS IS" BASIS,

  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

  See the License for the specific language governing permissions and

  limitations under the License.

  -->

  <web-app>

  <resource-ref>

  <description>DB Connection</description>

  <res-ref-name>jdbc/oracle</res-ref-name>

  <res-type>javax.sql.DataSource</res-type>

  <res-auth>Container</res-auth>

  </resource-ref>

  </web-app>

  test.jsp文件內容如下:

  <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

  <%@ page import="java.sql.*"%>

  <%@ page import="javax.sql.*"%>

  <%@ page import="javax.naming.*"%>

  <%

  try {

  Context initCtx = new InitialContext();

  Context envCtx = (Context) initCtx.lookup("java:comp/env");

  DataSource ds = (DataSource) envCtx.lookup("jdbc/oracle");

  Connection conn = ds.getConnection();

  Statement stmt = conn.createStatement();

  ResultSet rst = stmt.executeQuery("select stuAge from students");

  while (rst.next()) {

  out.println(rst.getInt(1));

  }

  conn.close();

  } catch (Exception e) {

  e.printStackTrace();

  }

  %>

  7.打開網頁http://localhost:8080/test/test.jsp

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章