zoukankan      html  css  js  c++  java
  • jdbc工具类

    public class DBUtils {
    
    //    static String user = "root";
    //    static String password = "root";
        static String user = "root";
        static String password = "root";
        static String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";
        static String driver = "com.mysql.cj.jdbc.Driver";
        static Connection conn = null;
        static ResultSet rs = null;
        static PreparedStatement ps = null;
    
        /**
         * 初始化数据库
         *
         * @return
         */
        public static Connection getConnection() {
            try {
                Class.forName(driver);
                conn = DriverManager.getConnection(url, user, password);
            } catch (Exception e) {
                System.out.println("数据库连接异常");
                e.printStackTrace();
            }
            return conn;
        }
    
        /**
         * 修改操作
         *
         * @param sql
         * @return
         */
        public static int addUpdDel(String sql) {
            int i = 0;
            try {
                PreparedStatement ps = conn.prepareStatement(sql);
                i = ps.executeUpdate();
            } catch (SQLException e) {
                System.out.println("sql数据库增删改异常");
                e.printStackTrace();
            }
            return i;
        }
    
        /**
         * 查询操作
         *
         * @param sql
         * @return
         */
        public static ResultSet selectSql(String sql) {
            try {
                ps = conn.prepareStatement(sql);
                rs = ps.executeQuery(sql);
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                System.out.println("sql数据库查询异常");
                e.printStackTrace();
            }
    
            return rs;
        }
    
    
        /**
         * 关闭连接
         *
         * @param conn
         */
        public static void closeConnection(Connection conn) {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    System.out.println("数据库关闭异常");
                    e.printStackTrace();
                }
            }
        }
    }
    

      

  • 相关阅读:
    2016年总结,不一样的2016
    appium 遇到的坑
    Python xml 解析百度糯米信息
    Python 3.4 链接mysql5.7 数据库使用方法
    python3.x爬取美团信息
    基于python3的手机号生成脚本
    python3.x 学习心得
    H3C SNMP OID
    jython获取was5.1的jvm监控参数
    使用Jyhon脚本和PMI模块监控WAS性能数据
  • 原文地址:https://www.cnblogs.com/padazala/p/15497429.html
Copyright © 2011-2022 走看看