JDBC工具类

public class JdbcUtil {
    private static final Properties ps = new Properties();

    private JdbcUtil() {
    }

    static {
        loadProperties();
    }

    private static void loadProperties() {
        try {
            InputStream is = JdbcUtil.class.getResourceAsStream("jdbc.properties");
            ps.load(is);
            String driverName = ps.getProperty("driver", "com.mysql.cj.jdbc.Driver");
            Class.forName(driverName);
        } catch (Exception e) {
            ps.setProperty("driver", "com.mysql.cj.jdbc.Driver");
            ps.setProperty("url", "jdbc:mysql://localhost:3306/test?serverTimezone=UTC");
            ps.setProperty("username", "root");
            ps.setProperty("password", "123456");
        }
    }

    public static Connection getConnection() throws Exception {
        if (ps.size() > 0) {
            String url = ps.getProperty("url");
            String username = ps.getProperty("username");
            String password = ps.getProperty("password");
            return DriverManager.getConnection(url, username, password);

        }
        return null;
    }

    public static PreparedStatement createStatement(Connection connection, String sql, Object... params) throws Exception {
        PreparedStatement ps = null;
        if (connection != null) {
            ps = connection.prepareStatement(sql);
            if (params != null && params.length > 0) {
                for (int i = 0; i < params.length; i++) {
                    ps.setObject(i + 1, params[i]);
                }
            }
        }
        return ps;
    }

    public static int executeUpdate(Connection connection, String sql, Object... params) throws Exception {
        int res = 0;
        if (connection != null) {
            PreparedStatement ps = createStatement(connection, sql, params);
            if (ps != null) {
                res = ps.executeUpdate();
            }
        }
        return res;
    }

    public static ResultSet executQuery(Connection connection, String sql, Object... params) throws Exception {
        ResultSet res = null;
        if (connection != null) {
            PreparedStatement ps = createStatement(connection, sql, params);
            if (ps != null) {
                res = ps.executeQuery();
            }
        }
        return res;
    }

    public static void close(ResultSet resultset, Statement statement, Connection connection) throws Exception {
        try {
            if (resultset != null) resultset.close();
        } finally {
            try {
                if (statement != null) statement.close();
            } finally {
                if (connection != null) connection.close();
            }
        }
    }
}

测试:

public class Test {
    public static void main(String[] args) throws Exception {
        Connection conn = JdbcUtil.getConnection();
        PreparedStatement ps = JdbcUtil.createStatement(conn, "select * from tbl_users");
        ResultSet rs = JdbcUtil.executQuery(conn, "select * from tbl_users");
        while (rs.next()) {
            Long id = rs.getLong("id");
            String username = rs.getString("username");
            String password = rs.getString("password");
            Date rdate = rs.getDate("rdate");
            Boolean sex = rs.getBoolean("sex");
            System.out.println(id + "\t" + username + "\t" + password + "\t" + rdate + "\t" + sex);
        }
        JdbcUtil.close(rs, ps, conn);
    }
}

  • 7
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值