import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
//純java 連接數據庫
public class T {
//數據庫連接字符竄
private final String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl" ;
//用戶
private final String uid = "root" ;
//密碼
private final String pwd = "root" ;
//數據庫驅動類
private final String drivername = "oracle.jdbc.driver.OracleDriver" ;
private Connection conn ;
private PreparedStatement pstms ;
private ResultSet rs ;
public void open() {
try {
Class. forName ( drivername );
conn = DriverManager. getConnection ( url , uid , pwd );
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 執行查詢
* */
public ResultSet executeQuery(String sql,String [] values){
try {
open();
pstms = conn .prepareStatement(sql);
if (values!= null ) {
for ( int i = 0; i < values. length ; i++) {
pstms .setString(i+1, values[i]);
}
}
rs = pstms .executeQuery();
} catch (Exception e) {
e.printStackTrace();
}
return rs ;
}
/**
* 執行更新
* */
public int executeUpdate(String sql,String [] values){
int row=0;
try {
pstms = conn .prepareStatement(sql);
if (values!= null ) {
for ( int i = 0; i < values. length ; i++) {
pstms .setString(i+1, values[i]);
}
}
row= pstms .executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
closeAll();
}
return row;
}
/***
* 關閉所有資源
* */
public void closeAll() {
try {
if ( rs != null ) {
rs .close();
}
if ( pstms != null ) {
pstms .close();
}
if ( conn != null ) {
conn .close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
使用JNDI 連接池:
1. 在Tomcat 的 / conf / context .xml文件配置如下:
<? xml version = '1.0' encoding = 'utf-8' ?>
< Context >
< WatchedResource > WEB-INF/web.xml </ WatchedResource >
< Resource name = "ojdbc/house" auth = "Container"
type = "javax.sql.DataSource" maxActive = "4"
maxIdle = "2" maxWait = "10000"
username = "house" password = "house"
driverClassName = "oracle.jdbc.driver.OracleDriver"
url = "jdbc:oracle:thin:@localhost:1521:orcl"
/>
</ Context >
2. 在Web 項目的: WebRoot/WEB-INF/web.xml 配置如下:
<? xml version = "1.0" encoding = "UTF-8" ?>
< web-app version = "2.5"
xmlns = "http://java.sun.com/xml/ns/javaee"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee ;
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" >
< resource-ref >
< description > DataSource </ description >
< res-ref-name > ojdbc /house </ res-ref-name >
< res-type > javax.sql.DataSource </ res-type >
< res-auth > Container </ res-auth >
</ resource-ref >
< welcome-file-list >
< welcome-file > index.jsp </ welcome-file >
</ welcome-file-list >
</ web-app >
注意:Tomcat 的 / conf / context .xml的 Resource 的name 值要和 Web項目的 WebRoot/WEB-INF/web.xml 的 res-ref-name 值一樣
3. Java代碼如下:
package com.house.dao.impl;
import java.sql.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class BaseDao {
private ResultSet rs = null;
private PreparedStatement pstms = null;
private Connection conn = null;
private static DataSource ds = null;
static {
try {
InitialContext cnt=new InitialContext();
ds=(DataSource)cnt.lookup("java:comp/env/ojdbc/house");
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void open() {
try {
conn = ds.getConnection();
} catch (Exception e) {
e.printStackTrace();
}
}
public void closeAll() {
try {
if (rs != null) {
rs.close();
}
if (pstms != null) {
pstms.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}