jdbc从基础到优化

Posted 小手撬动地球

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jdbc从基础到优化相关的知识,希望对你有一定的参考价值。

技术分享
package com.xk.demotest.tools;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class DaoDBConectTools {
    /*
    // 传统jdbc连接
    private static final String driverName = "com.mysql.jdbc.Driver";
    private static final String url = "jdbc:mysql://localhost:3306/20170626javaweb01";
    private static final String user = "root";
    private static final String password = "root";

    public DaoDBConectTools() {
        Connection connect = null;
        Statement state = null;
        try {
            Class.forName(driverName);
            connect = DriverManager.getConnection(url, user, password);
            state = connect.createStatement();
            String sql = "";
            state.executeQuery(sql);
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                state.close();
                connect.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
*/
    
    private static final Properties pro = new Properties(); // ①创建properties对象
    //加载配置文件和驱动
    static {
        InputStream iStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("config.properties"); // ②引入配置文件
        try {
            pro.load(iStream); // ②引入配置文件
            String driverName = pro.getProperty("driverName"); // ③加载驱动
            Class.forName(driverName);
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    
    //获取连接对像并创建连接
    public static Connection connection() {
        String url = pro.getProperty("url");
        String user = pro.getProperty("userName");
        String password = pro.getProperty("password");
        Connection connect = null;
        try {
            connect = DriverManager.getConnection(url, user, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connect;
    }
    
    //关闭数据库连接
    public static void close(Connection connect, Statement state) {
        if (state != null) {
            try {
                state.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (connect != null) {
            try {
                connect.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    

}
View Code
package com.xk.demotest.tools;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class DaoDBConectTools {
    /*
    // 传统jdbc连接
    private static final String driverName = "com.mysql.jdbc.Driver";
    private static final String url = "jdbc:mysql://localhost:3306/20170626javaweb01";
    private static final String user = "root";
    private static final String password = "root";

    public DaoDBConectTools() {
        Connection connect = null;
        Statement state = null;
        try {
            Class.forName(driverName);
            connect = DriverManager.getConnection(url, user, password);
            state = connect.createStatement();
            String sql = "";
            state.executeQuery(sql);
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                state.close();
                connect.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
*/
    
    private static final Properties pro = new Properties(); // ①创建properties对象
    //加载配置文件和驱动
    static {
        InputStream iStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("config.properties"); // ②引入配置文件
        try {
            pro.load(iStream); // ②引入配置文件
            String driverName = pro.getProperty("driverName"); // ③加载驱动
            Class.forName(driverName);
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    
    //获取连接对像并创建连接
    public static Connection connection() {
        String url = pro.getProperty("url");
        String user = pro.getProperty("userName");
        String password = pro.getProperty("password");
        Connection connect = null;
        try {
            connect = DriverManager.getConnection(url, user, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connect;
    }
    
    //关闭数据库连接
    public static void close(Connection connect, Statement state) {
        if (state != null) {
            try {
                state.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (connect != null) {
            try {
                connect.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    

}

 

以上是关于jdbc从基础到优化的主要内容,如果未能解决你的问题,请参考以下文章

从JVM的角度看JAVA代码--代码优化

说透MySQL:从数据结构到性能优化,附实际案例和面试题

MyBatis从入门到精通—MyBatis基础知识和快速入门

JDBC_基础6步骤- 及优化

部分代码片段

从 JDBC SQL SELECT 中省略列是过早的优化吗?