JDBCUtils工具类

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录


JDBCUtils工具类


package datasource.utils;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

public class JDBCUtils {
        //1.定义成员变量
        private static DataSource ds;

        static {
                try {
//                        System.out.println(JDBCUtils.class.getClassLoader().getResourceAsStream("durid.properties"));
                        //2.加载配置文件
                        Properties pro = new Properties();
                        pro.load(JDBCUtils.class.getClassLoader().getResourceAsStream("durid.properties"));
                        //3.获取DataSouce
                        ds = DruidDataSourceFactory.createDataSource(pro);
                } catch (IOException e) {
                        e.printStackTrace();
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }

        public static Connection getConnection() throws SQLException {
                //4.连接
                return ds.getConnection();
        }

        //归还资源
        public static void close(ResultSet rs,Statement stmt,Connection conn){
                if (rs != null){
                        try {
                                rs.close();//归还资源
                        } catch (SQLException e) {
                                e.printStackTrace();
                        }
                }

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

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

        public static void close(Statement stmt,Connection conn){
//                if (stmt != null){
//                        try {
//                                stmt.close();
//                        } catch (SQLException e) {
//                                e.printStackTrace();
//                        }
//                }
//
//                if (conn != null){
//                        try {
//                                conn.close();
//                        } catch (SQLException e) {
//                                e.printStackTrace();
//                        }
//                }
                close(null,stmt,conn);//简化代码
        }

        //获取连接池方法
        public static DataSource getDataSource(){
                return ds;
        }
}
增删查改方法:

package jdbctemplate;

import datasource.utils.JDBCUtils;
import domian.Emp;
import org.junit.Test;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.beans.Transient;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class JdbcTemplateDemo2 {

    // Junit单元测试可以让方法独立执行
    // 修改一条记录
    @Test
    public void test(){

        // 获取jdbc连接对象
        JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
        // 定义sql语句
        String sql = "update emp set name = ?,age = ? where id = ?";
        // 执行sql语句
        int count = template.update(sql,"成龙",100,1);
        System.out.println(count);
    }
    // 增加一条记录
    @Test
    public void test2(){
        //获取jdbc连接对象
        JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
        //定义sql语句
//        String sql = "insert into emp values(4,'荣誉','404')";
        String sql = "insert into emp values(?,?,?)";//防止sql注入
        // 执行sql语句
        int count = template.update(sql,5,"合法","1024");
        System.out.println(count);
    }
    JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    // 删除刚刚添加的记录
    @Test
    public void test3(){
        //获取jdbc连接对象
        JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
        String sql = "delete from emp where id = ?";//防止sql注入
        // 执行sql语句
        int count = template.update(sql,5);
        System.out.println(count);
    }

    // 查询一条记录将其封装为map集合
    @Test
    public void test4(){
        String sql = "select * from emp where id = ?";//防止sql注入

        Map<String, Object> maps = template.queryForMap(sql, 4);
        // 执行sql语句
        System.out.println(maps);
    }

    // 查询所有记录,将其封装为list
    @Test
    public void test5(){
        String sql = "select * from emp";
        List<Map<String, Object>> list = template.queryForList(sql);
        for (Map<String, Object> stringObjectMap: list) {
            System.out.println(stringObjectMap);
        }
    }

    // 查询所有记录,并将其封装为Emp对象的list集合
    @Test
    public void test6(){
        String sql = "select * from emp";
//        List<Emp> list = template.query(sql, new RowMapper<Emp>() {
//            @Override
//            public Emp mapRow(ResultSet rs, int i) throws SQLException {
//                Emp emp= new Emp();
//                int id = rs.getInt("id");
//                String username = rs.getString("username");
//                String password = rs.getString("password");
//
//                emp.setId(id);
//                emp.setNAME(username);
                emp.setNAME(password);
//                return emp;
//            }
//        });
        List<Emp> list = template.query(sql, new BeanPropertyRowMapper<Emp>(Emp.class));
        for (Emp emp: list) {
            System.out.println(emp);
        }
    }

    //查询总记录数
    @Test
    public void test7(){
        String sql = "select count(id) from emp";
        Long total = template.queryForObject(sql,Long.class);
        System.out.println(total);
        }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值