zoukankan      html  css  js  c++  java
  • Java-JDBC

    properties文件

    driver=com.mysql.cj.jdbc.Driver
    url=jdbc:mysql://localhost:3306/db_name?useUnicode=true&characterEncoding=utf8&useSSl=true&serverTimezone=UTC
    username=root
    password=123456
    

    工具类

    import java.io.IOException;
    import java.io.InputStream;
    import java.sql.*;
    import java.util.Properties;
    
    public class JdbcUtils {
    
        private static String driver = null;
        private static String url = null;
        private static String username = null;
        private static String password = null;
    
        protected static Statement state = null;
        protected static PreparedStatement ps = null;
        protected static ResultSet rs = null;
        protected static Connection conn = null;
    
        static {
            try {
                InputStream inputStream = JdbcUtils.class.getClassLoader().getResourceAsStream("db.properties");
                Properties properties = new Properties();
                properties.load(inputStream);
                driver = properties.getProperty("driver");
                url = properties.getProperty("url");
                username = properties.getProperty("username");
                password = properties.getProperty("password");
    
                Class.forName(driver);
    
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    
        public static synchronized Connection getConnection() {
    
            if(conn==null){
                try {
                    conn = DriverManager.getConnection(url, username, password);
                    System.out.println("连接成功!");
                } catch (Exception e) {
                    e.printStackTrace();
                    System.out.println("连接失败!");
                }
            }
            return conn;
        }
    
        /**
         * 执行insert,update,delete
         *
         * @param sql
         * @return
         */
        public static int executeUpdate(String sql) {
            int result = 0;
            try {
                state = getConnection().createStatement();
                result = state.executeUpdate(sql);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return result;
        }
    
        /**
         * 执行select
         *
         * @param sql
         * @return
         */
        public static ResultSet executeQuery(String sql) {
            try {
                state = getConnection().createStatement();
                rs = state.executeQuery(sql);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return rs;
        }
    
        /**
         * 返回动态sql语句
         *
         * @param sql
         * @return
         */
        public static PreparedStatement getPreparedStatement(String sql) {
            try {
                ps = getConnection().prepareStatement(sql);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return ps;
        }
    
        /**
         * 关闭连接
         *
         * @param
         * @return
         */
        public static void close() {
            try {
                if (rs != null)
                    rs.close();
                if (ps != null)
                    ps.close();
                if (state != null)
                    state.close();
                if (conn != null)
                    conn.close();
                System.out.println("成功关闭!");
            } catch (SQLException e) {
                e.printStackTrace();
                System.out.println("成功失败!");
            }
        }
    }
    
  • 相关阅读:
    topcoder srm 633 div1
    HDU 4997 Biconnected (状态压缩DP)
    HDU 5013 City Tour
    BZOJ 3672 [Noi2014]购票 (熟练剖分+凸壳维护)
    BZOJ 1488: [HNOI2009]图的同构 polay
    BZOJ 1565 植物大战僵尸(最大权闭合图)
    iOS加载程序视图的方式
    内存管理2(主讲MRR)
    内存管理1
    排序算法之希尔排序
  • 原文地址:https://www.cnblogs.com/zhang-han/p/14305329.html
Copyright © 2011-2022 走看看