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("成功失败!");
            }
        }
    }
    
  • 相关阅读:
    redhat 7.2 内网安装docker
    使用dockerfile 创建ubuntu ssh镜像
    docker 离线环境安装oracle
    redhat 6.6 、7、Centos7离线安装docker
    用命令行管理aws s3
    Anaconda介绍、安装及使用教程
    python2 编码问题万能钥匙
    从mongo数据库中导出数据的方法
    MongoDB学习第三篇 --- Insert操作
    MongoDB学习笔记(一)-Insert操作
  • 原文地址:https://www.cnblogs.com/zhang-han/p/14305329.html
Copyright © 2011-2022 走看看