jdbc,jndi连接oracle数据库

package util;

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

import model.SafeCard;

/**
 * 连接oracle数据库工具类
 * 需要ojdbc14.jar
 * @author C1700092
 * 1.加载oracel驱动
 * 2.获取连接
 * 3.执行sql
 *
 */
public class CopyOfConnectOracleUtil {
	//数据库地址
	private static final String URL = "jdbc:oracle:thin:@ip:port/DBDEV01";
	//数据库用户名
	private static final String USER_NAME = "admin";
	//数据库密码
	private static final String PASSWORD = "admin";
	
	/**
	 * 1.加载oracel驱动
     * 2.获取连接
     * 3.执行sql
     * 
     * 获取卡片信息
	 */
	public List<SafeCard> getCardList(String sql){
		Connection connection = null;
		Statement statement = null;
		ResultSet set = null;
		try {
			//1.
			Class.forName("oracle.jdbc.OracleDriver");
			//2.
			connection = DriverManager.getConnection(URL, USER_NAME, PASSWORD);
			statement = connection.createStatement();
			set = statement.executeQuery(sql);
			while (set.next()) {
				String id = set.getString("ID");
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				set.close();
				statement.close();
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return null;
	} 
}

jndi

package util;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.naming.CommunicationException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;


import model.SafeCard;
public class CopyOfConnectOracleUtil {
	public List<SafeCard> getCardList(String sql){
		List<SafeCard> list = new ArrayList<SafeCard>();
		Connection connection = null;
		Statement statement = null;
		ResultSet set = null;
		try {
			connection = getConnectionByJndi("balance");
			statement = connection.createStatement();
			set = statement.executeQuery(sql);
			while (set.next()) {
				SafeCard sc = new SafeCard();
				String id = set.getString("ID");
				String cardName = set.getString("CARD_NAME");
				String cardNum = set.getString("CARD_NUM");
				String sequenceNum = set.getString("SEQUENCE_NUM");
				sc.setId(id);
				sc.setCardName(cardName);
				sc.setCardNum(cardNum);
				sc.setSequenceNum(sequenceNum);
				list.add(sc);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (CommunicationException e) {
			e.printStackTrace();
		} catch (NamingException e) {
			e.printStackTrace();
		} finally {
			try {
				set.close();
				statement.close();
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return list;
	} 
	/**
	 * 使用jndi方式连接数据库
	 * @param jndi
	 * @return
	 * @throws SQLException
	 * @throws NamingException
	 * @throws CommunicationException
	 */
	public Connection getConnectionByJndi(String jndi) throws SQLException, NamingException,
	CommunicationException {
		DataSource ds = null;
		try {
			Context ctx = new InitialContext();
			try {
			ds = (DataSource) ctx.lookup(jndi);
			} catch (Exception ne1) {
			try {
			ds = (DataSource) ctx.lookup("java:/" + jndi);
			} catch (Exception ne2) {
			try {
			ds = (DataSource) ctx.lookup("java:comp/env/jdbc/" + jndi);
			} catch (Exception e5) {
			
			}
			}
			}
		} catch (NamingException ex) {
			ex.printStackTrace();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		Connection conn = null;
		try {
			if (ds != null) {
			conn = ds.getConnection();
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return conn;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值