码迷,mamicode.com
首页 > 数据库 > 详细

JDBC 工具类

时间:2019-09-26 23:07:52      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:property   tst   创建   getpath   driver   druid   lib   als   成员变量   

数据池+JdbcTemplate 工具类

package wdnmd.xswl;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class JDBCUtil {
    //定义成员变量DataSourse
    private static DataSource dataSource;


    static {
        try {
            //加载配置文件
            Properties properties = new Properties();
            properties.load(JDBCUtil.class.getClassLoader().getResourceAsStream("druid.properties"));
            //获取DataSource
            dataSource = DruidDataSourceFactory.createDataSource(properties);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    //获取连接
    public static Connection  getConnnection() throws SQLException {
        return dataSource.getConnection();
    }

    //释放资源
    public static void close(Statement statement,Connection connection){
       close(null,statement,connection);
    }

    public static void close(ResultSet resultSet, Statement statement, Connection connection){
        if (resultSet != null){
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (statement != null){
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if(connection != null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    //获取连接池的方法
    public static DataSource getDataSource(){
        return dataSource;
    }



}

properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/study?useSSL=true
username=wdnmd
password=123
initialSize=5
maxActive=10
maxWait=3000

测试

package cn.itcast.jdbctemplate;

import cn.itcast.utils.JDBCUtils;
import org.springframework.jdbc.core.JdbcTemplate;

public class JdbcTemplateDemo1 {

    public static void main(String[] args) {
        //1.导入jar包
        //2.创建JDBCTemplate对象
        JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
        //3.调用方法
        String sql = "update account set balance = 5000 where id = ?";
        int count = template.update(sql, 3);
        System.out.println(count);
    }
}

 


 

 

JDBC工具类

package wdnmd.xswl;

import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.sql.*;
import java.util.Properties;

public class JDBCUtils {

    private static String url;
    private static String username;
    private static String password;
    private static String driver;


    static {
        try {
            //读取资源文件,获取值
            Properties properties = new Properties();
            ClassLoader classLoader = JDBCUtils.class.getClassLoader();
            URL URL = classLoader.getResource("jdbc.properties");
            String path = URL.getPath();
            properties.load(new FileReader(path));

            //获取数据,赋值
            url = properties.getProperty("url");
            username = properties.getProperty("username");
            password = properties.getProperty("password");
            driver = properties.getProperty("Driver");
            Class.forName(driver);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }



    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url,username,password);
    }


    /*
        释放资源
     */
    public static void close(Statement statement,Connection connection){
        if (statement != null){
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (connection != null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void close(ResultSet resultSet, Statement statement, Connection connection){
        if (resultSet != null){
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (statement != null){
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (connection != null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void close(ResultSet resultSet, Connection connection){
        if (resultSet != null){
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }


        if (connection != null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

properties

Driver = com.mysql.jdbc.Driver
url = jdbc:mysql://localhost:3306/study?useSSL=true
username = wdnmd
password = 123

 

测试

package wdnmd.jdbc;

import wdnmd.xswl.JDBCUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

public class JDBCDemo03 {

    public static void main(String[] args) throws SQLException {
        Connection connection = JDBCUtils.getConnection();
        String sql = "select * from sort";
        ResultSet resultSet = connection.prepareStatement(sql).executeQuery();
        while (resultSet.next()){
            System.out.println(resultSet.getString("sname"));
        }
        JDBCUtils.close(resultSet,connection);
    }
}

 

JDBC 工具类

标签:property   tst   创建   getpath   driver   druid   lib   als   成员变量   

原文地址:https://www.cnblogs.com/viperqy/p/11594961.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!