关于JDBC的简单封装

   最近公司在做一个项目是关于一个省的大数据同计类的数据可视化系统,系统操作单表百万级的数据查改操作,前期使用公司的基础框架会产生内存溢出,因为导出的库不支持分页处理,所以就相当与数据的一次性导入。在缺少可用的框架的情况下尝试了无数次才发现还是手写的JDBC最好用,但好久没用了,不会封装了,今天写这个作为本人总结吧!!

  1. 首先数据源使用properties文件读取
DBdriver=oracle.jdbc.driver.OracleDriver
DBconnstr=jdbc:oracle:thin:@localhost:1521:orcl
DBuser=LYSJCK
DBpwd=123456

读取方法

public String getXMLPath(){
            String rslt = null;
            try {
                rslt = this.getClass().getResource("").toURI().getPath().substring(1);
            } catch (URISyntaxException e) {
                e.printStackTrace();
                rslt = this.getClass().getResource("").getPath().substring(1).replaceAll("%20", " ");
            }
            rslt = rslt.substring(0,rslt.replace('\\','/').indexOf("×××××××"));
            return rslt;
        }

//获取WEB-INF下的文件配置路径

private String getCofig(){

        String xmlPath = confReader.getXMLPath();
        String path=xmlPath+"config/TongDaHaiDBConfig.properties";
        return path;
    }

//将上面方法获取到的路径放入本方法处理得到文件  再讲

文件处理放入Properties

public static Properties getProper(String path) throws Exception{
        File file=new File(path);
        FileReader fileReader = new FileReader(file);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        Properties pro = new Properties();
        pro.load(bufferedReader);
        return pro;
    }

//将获取到的 propretitesfa放入方法中的到链接

    public  Connection getCon(String path){
        String getPath = this.GetPath()+path;
        Properties pro = this.getProperties(getPath);
        Connection connection=null;
        try {
            getClass().forName(String.valueOf(pro.getProperty("DBdriver")));
             connection = DriverManager.getConnection(String.valueOf(pro.get("DBconnstr")), String.valueOf(pro.get("DBuser")), String.valueOf(pro.get("DBpwd")));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return connection;
    }

//接下来是对操作数据库的简单封装

package com.liyu;

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



public class LiYuQueary {
	private Connection con=null;
	private static LiYuQueary instance=null;
	//构造函数
	public LiYuQueary(){
		Connection con = getCon("config/Orcal.properties");
	}
	//单例模式
	public static LiYuQueary newInstance(){
		if(instance==null)instance = new LiYuQueary();
		return instance;
	}
	
//获取链接
	public static Connection getCon(String path){
		LiYuJDBC liYuJDBC = new LiYuJDBC();
		return liYuJDBC.getCon(path);
	}
	
//返回List类型的查询结果
	public List<Map> liYuQueary(String sql){
		Statement cst = null;
		ResultSet rslt = null;
		ArrayList<Map> lst = new ArrayList<Map>();
		try {
			cst = con.createStatement();
			 rslt = cst.executeQuery(sql);
			ResultSetMetaData metaData = rslt.getMetaData();
			int columnCount = metaData.getColumnCount();
			while (rslt.next()) {
				HashMap map = new HashMap();
				for(int i=0;i<columnCount;i++){
					map.put(metaData.getColumnName(i), rslt.getObject(i));
				}
				lst.add(map);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				if (rslt != null) {
					rslt.close();
				}
				if (cst != null) {
					cst.close();
				}
				if (con != null) {
					con.close();
				} 
			} catch (Exception e2) {
			}
		}
		return lst;
	}
	//处理增删改
	public int liYuUpdate(String sql){
		Statement cst = null;
		int rslt = 0;
		ArrayList<Map> lst = new ArrayList<Map>();
		try {
			cst = con.createStatement();
			 rslt = cst.executeUpdate(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				if (cst != null) {
					cst.close();
				}
				if (con != null) {
					con.close();
				} 
			} catch (Exception e2) {
			}
		}
		return rslt;
	}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值