10.3 JDBC 工具类`BaseDao.java`

10.3 JDBC 工具类BaseDao.java

package xuanzishare.dao;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class BaseDao {
    public static String driver;
    // 数据库驱动

    public static String url;
    // url

    public static String user;
    // 数据库用户名

    public static String password;
    // 数据库密码

    static {
        //静态代码块,在类加载的时候执行
        init();
    }

    public static void init() {
        String configFile = "database.properties";
        //配置文件路径
        //加载配置文件到输入流中
        InputStream is = BaseDao.class.getClassLoader().getResourceAsStream(configFile);
        Properties params = null;
        try {

            params = new Properties();
            //从输入流中读取属性列表
            params.load(is);

        } catch (IOException e) {
            e.printStackTrace();
        }
        //根据指定的获取对应的值
        driver = params.getProperty("driver");
        url = params.getProperty("url");
        user = params.getProperty("user");
        password = params.getProperty("password");
    }

    /**
     * 得到数据库连接
     *
     * @return 数据库连接
     */

    public static Connection getConn() {

        Connection conn = null;
        try {
            Class.forName(driver);
            // 注册驱动
            conn = DriverManager.getConnection(url, user, password);
            // 获得数据库连接
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        return conn;
        // 返回连接
    }

    public static void closeAll(Connection conn, PreparedStatement ps, ResultSet rs) {

        /* 如果rs不空,关闭rs */
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        /* 如果pstmt不空,关闭pstmt */
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        /* 如果conn不空,关闭conn */
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }

    public int updateBuffer(String sql, Object[] objects) {
        Connection conn = null;
        PreparedStatement ps = null;
        int i = 0;
        try {
            conn = getConn();
            ps = conn.prepareStatement(sql);
            if (objects != null) {
                for (int j = 0; j < objects.length; j++) {
                    ps.setObject(j + 1, objects[j]);
                }
            }
            System.out.println("\u001B[34mupdateBuffer:" + ps + "\u001B[0m");
            i = ps.executeUpdate();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        closeAll(conn, ps, null);
        return i;
    }

    public double selectAllId(String sql, Object[] objects) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        double allId = 0;
        try {
            conn = getConn();
            ps = conn.prepareStatement(sql);
            if (objects != null) {
                for (int i = 0; i < objects.length; i++) {
                    ps.setObject(i + 1, objects[i]);
                }
            }
            System.out.println("\u001B[34mselectAllId:" + ps + "\u001B[0m");
            rs = ps.executeQuery();
            allId = 0;
            while (rs.next()) {
                allId = rs.getDouble(1);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        closeAll(conn, ps, rs);
        return allId;
    }
//    只查询数字类型 把值返还回去
}

你可能感兴趣的:(MySQL,数据库,mysql,java)