简单的JDBCUtil

学了JDBC ,传一下自己封装的JDBCUtil

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

/**
 * @Author: tjx
 * @Date: 2019/8/4 9:24
 */
public class MyJDBC {
    private static  String DRIVER;
    private static String URL;
    private static String USER;
    private static String PASSWORD;
    private static File file=null;
    static{
        file=new File("myjdbc.properties");
        Properties properties=new Properties();
        if(!file.exists()){
            properties.setProperty("mysqlUSER","root");
            properties.setProperty("mysqlDRIVER","com.mysql.jdbc.Driver");
            properties.setProperty("mysqlURL","jdbc:mysql://localhost:3306/school?useUnicode=true&characterEncoding=utf8");
            properties.setProperty("mysqlPWD","root");
            try {
                properties.store(new FileOutputStream(file),"myjdbc");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            properties.load(new FileInputStream(file));
            DRIVER= properties.getProperty("mysqlDRIVER");
            URL=properties.getProperty("mysqlURL");
            USER=properties.getProperty("mysqlUSER");
            PASSWORD=properties.getProperty("mysqlPWD");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static Connection getConn(){
        Connection connection=null;
        try{
            Class.forName(DRIVER);
            connection= DriverManager.getConnection(URL,USER,PASSWORD);
        }catch (Exception e){
            e.printStackTrace();
        }
        return connection;
    }

    public static int update(String sql,Object...obj){
        Connection connection=getConn();
        CallableStatement cs=null;//使用SQL存储过程
        int result=0;
        try {
            cs=connection.prepareCall(sql);
            for (int i = 0; i <obj.length ; i++) {
                cs.setObject(i+1,obj[i]);
            }
            result=cs.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            close(null,cs,connection);
        }
        return result;
    }
    public static int updatePS(String sql,Object...obj){
        Connection connection=getConn();
        PreparedStatement ps=null;//防止SQL注入
        int result=0;
        try {
            ps=connection.prepareStatement(sql);
            for (int i = 0; i <obj.length ; i++) {
                ps.setObject(i+1,obj[i]);
            }
            result=ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            close(null,ps,connection);
        }
        return result;
    }
    public static int updateS(String sql){
        Connection connection=getConn();
        Statement s=null;//传入普通的SQL语句
        int result=0;
        try {
            result=s.executeUpdate(sql);
        }catch (SQLException sqle){
            sqle.printStackTrace();
        }finally {
            close(null,s,connection);
        }
        return result;
    }
    public static <T> List<T> queryToClass(String sql,Class<T> c,Object...obj){
        List<T> list=new ArrayList<T>();
        Object object[]=obj;
        ResultSet resultSet=query(sql,object);
        try {
            while (resultSet.next()){
                T bean=c.newInstance();
                Field field[]=c.getDeclaredFields();//获得所有属性
                for (int i=0;i<field.length;i++) {
                    String fieldName=field[i].getName();//获得属性名
                    Object fieldValue=resultSet.getObject(i+1);//获得对应列的值
                    String set="set"+fieldName.substring(0,1).toUpperCase()+fieldName.substring(1);
                    Method setMethod=c.getDeclaredMethod(set,field[i].getType());
                    setMethod.invoke(bean,fieldValue);
                 }
                 list.add(bean);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
    public static ResultSet query(String sql,Object...obj){//对查询语句简单封装
        Connection connection=getConn();
        PreparedStatement ps=null;
        ResultSet resultSet=null;
        try {
            ps=connection.prepareStatement(sql);
            for (int i = 0; i <obj.length ; i++) {
                ps.setObject(i+1,obj[i]);
            }
            resultSet=ps.executeQuery();

        } catch (SQLException e) {
            e.printStackTrace();
        }//没有关闭对连接进行关闭
        return resultSet;
    }
       public static void close(ResultSet resultSet, Statement statement, Connection connection){
        if (resultSet!=null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(statement!=null){
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(connection!=null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        System.out.println(getConn());//判断是否获取了连接
        String sql="select * from dept";
        ResultSet resultSet=query(sql);//测试一下查询
        try {
            while (resultSet.next()){//多条语句使用while循环判断
              System.out.println(resultSet.getInt(1)+"\t"+resultSet.getString(2)+"\t"+resultSet.getString(3));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        String sql1="select * from dept where dname like?";
        //ResultSet resultSet=query(sql1);
        List<Dept> list=queryToClass(sql,Dept.class,"%S%");
        System.out.println(list);
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值