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;
}
}