jdbc使用的具体案例

一、jdbc使用的案例

1.工具类

package com;

import java.io.FileInputStream;
import java.sql.*;
import java.util.Properties;

//1.获取连接对象 2.关闭连接对象---取代单元测试中的 @Before 和 @After
public class JdbcUtil {
   
    private static Properties p;

    static {
   
        //1.读取jdbc.properties文件
        try{
   
            FileInputStream is = new FileInputStream("D:\\java0104\\jdbc--day05\\src\\jdbc.properties");
            p = new Properties();
            p.load(is);
        }catch (Exception e){
   
            e.printStackTrace();
        }
    }
    //1.获取连接对象
    public static Connection getCon(){
   
        try{
   
            return DriverManager.getConnection(p.getProperty("url"),p);
        }catch (Exception e){
   
            e.printStackTrace();
        }
        return null;
    }
    //2.释放资源
    public static void close(ResultSet rs, Statement state,Connection con){
   
        if (rs!=null){
   
            try {
   
                rs.close();
            } catch (SQLException e) {
   
                e.printStackTrace();
            }
        }
        if (state!=null){
   
            try {
   
                state.close();
            } catch (SQLException e) {
   
                e.printStackTrace();
            }
        }
        if (con!=null){
   
            try {
   
                con.close();
            } catch (SQLException e) {
   
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
   
        System.out.println(getCon());
    }
}

2.查询一个表中所有的数据,得到一个结果集,封装结果集到list集合,并在控制台输出

package lei;

import java.sql.Date;

public class Emp {
   
    private int id;
    private String name;
    private String gender;
    private double salary;
    private Date join_date;
    private int dept_id;

    public Emp() {
   
    }

    @Override
    public String toString() {
   
        return "Emp{" +
                "id=" + id +
                ", name='" + name + 
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值