分享

java程序连接ORACLE 10g 数据库与操作

 昵称7172177 2011-06-19
package com.wh.dao;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.wh.util.ConnectionManager;
public class BaseDao {
private static BaseDao instance = new BaseDao();
private BaseDao(){};
public static BaseDao getInstance(){
return instance;
}
public void closeAll(Connection conn,PreparedStatement pstmt,Statement stmt,ResultSet res){
if(res!=null){
try {
res.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(pstmt!=null){
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* 通用增删改
* @param sql
* @param params
* @return
*/
public int executeUpdate(String sql,String[] params){
Connection conn = null;
Statement stmt = null;
PreparedStatement pstmt = null;
int result = 0;
try {
conn = ConnectionManager.getJDNIConnection();
if(params==null||params.length==0){
stmt = conn.createStatement();
result = stmt.executeUpdate(sql);
}else{
pstmt = conn.prepareStatement(sql);
this.setParam(pstmt, params);
result = pstmt.executeUpdate();
}
} catch (SQLException e) {
e.printStackTrace();
} finally{
this.closeAll(conn, pstmt, pstmt, null);
}
return result;
}
/**
* 通用查询
* @param className
* @param sql
* @param params
* @return
*/
public List<Object> executeQuery(Class<?> className,String sql,String[] params){
List<Object> list = new ArrayList<Object>();
Connection conn = null;
Statement stmt = null;
PreparedStatement pstmt = null;
ResultSet res = null;
try {
conn = ConnectionManager.getJDNIConnection();
if(params==null||params.length==0){
stmt = conn.createStatement();
res = stmt.executeQuery(sql);
}else{
pstmt = conn.prepareStatement(sql);
this.setParam(pstmt, params);
res = pstmt.executeQuery();
}
ResultSetMetaData rsmd = res.getMetaData();
while(res.next()){
Object obj = className.newInstance();
Method[] methods = this.getMethods(className, rsmd);
for(int i = 0;i<methods.length;i++){
methods[i].invoke(obj, res.getObject(i+1));
}
list.add(obj);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} finally{
this.closeAll(conn, pstmt, pstmt, res);
}
return list;
}
/**
* 设置参数
* @param pstmt
* @param params
* @throws SQLException
*/
public void setParam(PreparedStatement pstmt,String[] params) throws SQLException{
for(int i = 0;i<params.length;i++){
pstmt.setString((i+1), params[i]);
}
}
/**
* 获取所有方法
* @param className
* @param rsmd
* @return
* @throws SQLException
* @throws SecurityException
* @throws NoSuchMethodException
* @throws ClassNotFoundException
*/
public Method[] getMethods(Class<?> className,ResultSetMetaData rsmd) throws SQLException, SecurityException, NoSuchMethodException, ClassNotFoundException{
String[] methodNames = new String[rsmd.getColumnCount()];
Method[] methods = new Method[rsmd.getColumnCount()];
for(int i = 0;i<rsmd.getColumnCount();i++){
String[] splitName = rsmd.getColumnName((i+1)).split("_");
methodNames[i] ="set";
for(String str : splitName){
methodNames[i] += (str.charAt(0)+"").toUpperCase()+str.substring(1).toLowerCase();
}
methods[i] =  className.getDeclaredMethod(methodNames[i], Class.forName(rsmd.getColumnClassName(i+1)));
}
return methods;
}
}
package com.wh.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnectionManager {
private static final String DRIVER = "oracle.jdbc.driver.OracleDriver";
private static final String URL = "jdbc\:oracle\:thin\:@127.0.0.1\:1521\:你的数据库实例名"
private static final String  USERNAME = "用户名";
private static final String PASSWORD = "密码";private ConnectionManager(){}
public static Connection getConnection() throws ClassNotFoundException, SQLException{
Class.forName(DRIVER);
return DriverManager.getConnection(URL,USERNAME,PASSWORD);
}
给你两个类,第一个是执行增删改查操作的BaseDAO类,第二个是连接管理类! 希望你有用!

追问

谢谢你的无私帮助,不过去行不了,第一个编译时有错误conn = ConnectionManager.getJDNIConnection();就是这个地方!如果把getJDNIConnection改成getConnection后就正确了,不过没法运行

回答

哦哦, 呵呵,我大意了,这是我原先自己写的一个用普通JDBC连接和JNDI连接, 改成getConnection之后还是无法运行吗?  报的是什么错?  还有,上面的那个是通用的查询和通用的增删改, 如果使用的时候你的model层的的某些类型有限制,这也是最不好的,比如oracle中number 类型要用BigDecimal 。。。等...

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多