JAVA JDBC工具类

Posted viperqy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA JDBC工具类相关的知识,希望对你有一定的参考价值。

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工具类的主要内容,如果未能解决你的问题,请参考以下文章

Java操作数据库(四,JDBC的工具类封装及测试)

JDBC工具类

JDBC工具类

Java工具类及配置文件模板大全

Java-jdbc工具类jdbcUtil

Java jdbc 连接oracle之三(封装工具类)