栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

properties文件的用法;utils类封装数据库连接、资源关闭、通用的增删改,以及两种通用的查询方法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

properties文件的用法;utils类封装数据库连接、资源关闭、通用的增删改,以及两种通用的查询方法

某*.properties文件内容例如:

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://1.14.XXX.XX:3306/smart_search?useUnicode=true&characterEncoding=utf-8
username=root
password=159357

若是普通javaweb项目,properties文件放在src中

若是maven项目,properties文件放在resources文件中

使用方法在如下方法封装中已体现

1.创建utils包-->创建JDBCutils类

2.封装静态方法,具体如下

//获取数据库链接
public static Connection getConnection() throws Exception {
    ClassLoader classLoader= JDBC.class.getClassLoader();
    InputStream in=classLoader.getResourceAsStream("db.properties");
    Properties pop=new Properties();
    pop.load(in);
    String driver=pop.getProperty("driver");
    String url=pop.getProperty("url");
    String dbUserName=pop.getProperty("username");
    String dbPassword=pop.getProperty("password");
    Class.forName(driver);
    Connection connection= DriverManager.getConnection(url,dbUserName,dbPassword);
    return connection;
}
//关闭链接和PreparedStatement
public static void closeResource(Connection conn, PreparedStatement ps){
    try{
        if(ps!=null)
            ps.close();
    }catch (SQLException e){
        e.printStackTrace();
    }

    try{
        if(conn!=null)
            conn.close();
    }catch (SQLException e){
        e.printStackTrace();
    }
}
//关闭链接和PreparedStatement和ResultSet
public static void closeResource(Connection conn, PreparedStatement ps, ResultSet rs){
    try{
        if(ps!=null)
            ps.close();
    }catch (SQLException e){
        e.printStackTrace();
    }

    try{
        if(conn!=null)
            conn.close();
    }catch (SQLException e){
        e.printStackTrace();
    }

    try{
        if(rs!=null)
            rs.close();
    }catch (SQLException e){
        e.printStackTrace();
    }
}
//通用的增删改,这里不建议void,因为要判断是否执行成功

public static void update(String sql,Object... args){
    Connection conn=null;
    PreparedStatement ps=null;
    try{
        conn=JDBC.getConnection();
        ps=conn.prepareStatement(sql);
        for(int i=0;i
//通用的查询(能够针对不同表、查询一条记录、返回一个Bean、动态的)

public static  T selectForBean(Class clazz,String sql,Object... args){
    Connection conn=null;
    PreparedStatement ps=null;
    ResultSet rs=null;
    try{
        conn=JDBC.getConnection();
        ps=conn.prepareStatement(sql);
        for(int i=0;i

//通用的查询(能够针对不同表、用于查询多条记录、返回一个集合、动态的)
public static   List selectForList(Class clazz, String sql, Object... args){
    Connection conn=null;
    PreparedStatement ps=null;
    ResultSet rs=null;
    try{
        conn=JDBC.getConnection();
        ps=conn.prepareStatement(sql);
        for(int i=0;i list=new ArrayList();
        while (rs.next()){
            //获取对应的无参构造器
            T t=clazz.newInstance();
            //处理结果集一行数据中的每一个列
            for(int i=0;i 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/735045.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号