• 连接数据库


    DBUtils.java

    package com.oracle.tools;
    
    import java.sql.Connection;
    import java.sql.SQLException;
    
    import javax.sql.DataSource;
    
    import org.apache.tomcat.dbcp.dbcp.BasicDataSource;
    
    public class DBUtils {
        public static final String DRIVER = "com.mysql.jdbc.Driver";
        public static final String URL = "jdbc:mysql://localhost:3306/project?characterEncoding=utf8";
        public static final String USERNAME = "root";
        public static final String PASSWORD = "123456";
        /*
         * 创建连接池BasicDataSource
         */
        public static BasicDataSource dataSource = new BasicDataSource();
        //静态代码块
        static {
            //对连接池对象 进行基本的配置
            dataSource.setDriverClassName(DRIVER); // 这是要连接的数据库的驱动
            dataSource.setUrl(URL); //指定要连接的数据库地址
            dataSource.setUsername(USERNAME); //指定要连接数据的用户名
            dataSource.setPassword(PASSWORD); //指定要连接数据的密码
        }
        /*
         * 返回连接池对象
         */
        public static DataSource getDataSource(){
            return dataSource;
        }
        //从连接池获取一个连接
        public static Connection getConn(){
            Connection conn=null;
            try {
                conn= dataSource.getConnection();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return conn;
        }
    
    }

    JDBCUtils.java

    package com.oracle.tools;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    public class JDBCUtils {
        public static Connection getConn(){
            Connection conn=null;
            try {
                Class.forName("com.mysql.jdbc.Driver");
                //获取链接
                String url="jdbc:mysql://localhost:3306/project?useUnicode=true&characterEncoding=utf8&autoReconnect=true&rewriteBatchedStatements=TRUE";
                String user="root";
                String password="123456";
                conn=DriverManager.getConnection(url, user, password);
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return conn;
        }
        public static void close(ResultSet rs,Statement sta,Connection conn){
            if(rs!=null){
                try {
                    rs.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(sta!=null){
                try {
                    sta.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        public static void close(Statement sta,Connection conn){
            if(sta!=null){
                try {
                    sta.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
    
        }
    }
  • 相关阅读:
    kettle安装及初步使用
    Datax初步使用
    可修改性及其实现战术(hot words)
    淘宝网的六个质量属性
    python数据可视化笔记---——matplotlib.pyplot()
    Linux虚拟环境virtualevn
    deepin安装虚拟环境virtualenv
    python面试题-web后端
    rabbitmq和redis用作消息队列的区别
    nginx配置项概括说明
  • 原文地址:https://www.cnblogs.com/dk2557/p/9772049.html
Copyright © 2020-2023  润新知