JDBC应用(Mysql)

1.首先要确保你的mysql是打开的状态,打开idea创建maven工程。

接下来就是在工程pom.xml里面打jar包

 jar包打完,在你左侧External Libraries目录下会有相对应的jar包,我这里打的jar包是mysql-connector-java:5.1.47。

2.创建datasource.porperties文件,里面有我们需要的一些配置信息,要记住文件的根目录。

 

 我这里datasource.porperties文件的根目录是

D:\\IdeaProjects\\conf\\datasource.properties

3.进入正题,敲代码,创建一个Dao类。

import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.*;
import java.util.*;

/**
 * @Author HanLiang
 * @Date 2021/12/9
 * @Description
 */
public class Dao {
    private static Properties prop;

    public static void init(String path) throws IOException, ClassNotFoundException {
        prop = new Properties();
        prop.load(new FileReader(path));
        Class.forName(prop.getProperty("mysql.driver"));
    }

    private static Connection con() throws SQLException {
        return DriverManager.getConnection(prop.getProperty("mysql.url"),
                prop.getProperty("mysql.username"), prop.getProperty("mysql.password"));
    }

    private static PreparedStatement pst(Connection con, String sql, Object... params) throws SQLException {
        PreparedStatement pst = con.prepareStatement(sql);
        for (int i = 0; i < params.length; i++) {
            pst.setObject(i + 1, params[i]);
        }
        return pst;
    }

    private static void close(AutoCloseable... closes) {
        for (AutoCloseable close : closes) {
            if (null != close) {
                try {
                    close.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static int exeUpdate(String sql, Object... params) {//增删改的方法
        int rst = -1;
        Connection con = null;
        PreparedStatement pst = null;
        try {
            con = con();
            pst = pst(con, sql, params);
            rst = pst.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rst;
    }
  
    private static Map<String, Method> parseClass(Class c){
        Map<String,  Method> map = new HashMap<>();
        for (Method method : c.getMethods()) {
            String name = method.getName();
            if (name.startsWith("set")){
                name = name.substring(3);
                name=name.substring(0,1).toLowerCase()+name.substring(1);
                map.put(name,method);
            }
        }
        return map;
    }
   
    private static  <T> T parseObject(Class<T> c,Map<String,Method> map,ResultSet rst) throws IllegalAccessException, InstantiationException, SQLException, InvocationTargetException {
        T t = c.newInstance();
        for (Map.Entry<String, Method> e : map.entrySet()) {//增强for遍历
            e.getValue().invoke(t,rst.getObject(e.getKey()));

        }
        return t;
    }
    //查询的方法
    public static<T> List<T> exeQurey(Class<T> c,String sql,Object...params){
        List<T> list = new ArrayList<>();
        Connection con=null;
        PreparedStatement pst=null;
        ResultSet rst=null;
        try {
            con=con();
            pst=pst(con,sql,params);
             rst = pst.executeQuery();
            Map<String, Method> map = parseClass(c);
            while (rst.next()) {
                list.add(parseObject(c,map,rst));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            close(rst,pst,con);
        }
       return list;
    }

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        init("D:\\IdeaProjects\\conf\\datasource.properties");
        List<Teacher> list = exeQurey(Teacher.class, "select * from teacher");
        for (Teacher e : list) {
            System.out.println(e);
        }
    }
}

我们在main方法里调用l查询的方法,需要一个Teacher实体类,实体类对应数据库里面的表格,实体类的属性即是表格的字段,表格里一行数据就是实体类的一个对象。我们必须要xian创建一个实体类:

/**
 * @Author HanLiang
 * @Date 2021/12/9
 * @Description
 */
public class Teacher {
    private int tid;
    private String tname;
    private String Gender;

    public Teacher() {
    }

    public Teacher(int tid, String tname, String gender) {
        this.tid = tid;
        this.tname = tname;
        Gender = gender;
    }

    public int getTid() {
        return tid;
    }

    public void setTid(int tid) {
        this.tid = tid;
    }

    public String getTname() {
        return tname;
    }

    public void setTname(String tname) {
        this.tname = tname;
    }

    public String getGender() {
        return Gender;
    }

    public void setGender(String gender) {
        Gender = gender;
    }

    @Override
    public String toString() {
        return "Teacher{" +
                "tid=" + tid +
                ", tname='" + tname + '\'' +
                ", Gender='" + Gender + '\'' +
                '}';
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值