0
回答
JDBC与Oracle数据库连接最常用方法
终于搞明白,存储TCO原来是这样算的>>>   
1:直接连接读取数据库内容
package com.jzh.DButil;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBConnection {
	public static Connection getConnection()
	{
		Connection conn=null;
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			String url="jdbc:oracle:thin:@localhost:1521:oracle";
			String uid = "scott";
			String pwd = "tiger";
			conn = DriverManager.getConnection(url, uid, pwd);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}
	public static void close(Connection conn)
	{
		if(conn==null)
		{
			return;
		}
		try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
	}
	public static void close(Statement stmt)
	{
		if(stmt==null)
		{
			return;
		}
		try {
			stmt.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public static void close(ResultSet  rs)
	{
		if(rs==null)
		{
			return;
		}
		try {
			rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}

2:加载。properties配置文件
package com.neusoft.ums.jing;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class GetConnection {

	
	public Connection getConn()
	{
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");// 返回与带有给定字符串名的类或接口相关联的 Class 对象
			} catch (ClassNotFoundException e) {
		e.printStackTrace();
		}
		Properties p=new Properties();//Properties 类表示了一个持久的属性集
		InputStream in=this.getClass().getResourceAsStream("sql.properties");
		try 
		{
			p.load(in);//加载配置文件
		} catch (IOException e) {
			e.printStackTrace();
		}
		String ip=p.getProperty("ip");
		String port=p.getProperty("port");
		String sid=p.getProperty("sid");
		String user=p.getProperty("name");
		String password=p.getProperty("pass");
		String url="jdbc:oracle:thin:@"+ip+":"+port+":"+sid;
		Connection conn=null;
		try {
			conn = DriverManager.getConnection(url,user,password);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}
	public void close( Connection conn)
	{
		if(conn!=null)
		{
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

}

3:通过数据库连接池
 首先要C:\apache-tomcat-6.0.14\conf下配置content.xml文件
<Resource name="conn/test" auth="Container"	type="javax.sql.DataSource"	maxActive="20"	maxIdel="10"	maxWait="100"	username="scott"	password="tiger"	driverClassName="oracle.jdbc.driver.OracleDriver"	url="jdbc:oracle:thin:@localhost:1521:oracle">
	</Resource>

 其次在WEB_INF/web.xml中添加
<resource-ref>
  	<res-ref-name>conn/test</res-ref-name>
  			<res-type>javax.sql.DataSource</res-type>
  	<res-auth>Container</res-auth>
 </resource-ref>

最后:
try {
			Context ct=new InitialContext();
			DataSource ds=(DataSource)ct.lookup("java:comp/env/conn/test");
			Connection conn=ds.getConnection();
			if(conn!=null)
			{
				System.out.println("成功");
			}
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}

原文链接:http://blog.csdn.net/jzh440/article/details/7536896
举报
晨曦之光
发帖于5年前 0回/161阅
顶部