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

JAVA JDBC工具类

时间:2019-09-21 19:20:09      阅读:71      评论:0      收藏:0      [点我收藏+]

标签:for   trace   ade   获取数据   void   取值   mys   获取   query   

JDBC工具类


 

创建一个properties 文件

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

 


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();
            }
        }
    }


}

测试

 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);

    }

 

JAVA JDBC工具类

标签:for   trace   ade   获取数据   void   取值   mys   获取   query   

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

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