All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.lq.cli.jdbc.JdbcUtil Maven / Gradle / Ivy

There is a newer version: 1.2.RELEASE
Show newest version
package com.lq.cli.jdbc;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class JdbcUtil {

    private JdbcConfig jdbcConfig;

    public JdbcUtil(JdbcConfig jdbcConfig) {
        try {
            this.jdbcConfig = jdbcConfig;
            Class.forName(jdbcConfig.getDriverClassName());
        } catch (ClassNotFoundException e) {
            throw new ExceptionInInitializerError(e);
        }
    }


    public Connection connection() throws SQLException {
        return DriverManager.getConnection(jdbcConfig.getUrl(), jdbcConfig.getUsername(), jdbcConfig.getPassword());
    }

    public boolean update(String sql) throws SQLException {
        Connection connection = connection();
        PreparedStatement statement = connection.prepareStatement(sql);
        boolean b = statement.executeUpdate() > 0;
        closeConnection(connection);
        closeStatement(statement);
        return b;
    }

    public boolean ddl(String sql) throws SQLException {
        Connection connection = connection();
        Statement statement = connection.createStatement();
        boolean execute = statement.execute(sql);
        closeConnection(connection);
        closeStatement(statement);
        return execute;
    }

    public List queryTableInfo() throws SQLException {
        List tableInfos = new ArrayList<>();
        Connection connection = connection();
        PreparedStatement statement = connection.prepareStatement("show TABLES");
        ResultSet resultSet = statement.executeQuery();
        while (resultSet.next()){
            List filedEntities = new ArrayList<>();
            String tableName = resultSet.getString(1);
            String tableFieldSql = selectTableFieldSql(tableName);
            PreparedStatement st = connection.prepareStatement(tableFieldSql);
            ResultSet rs = st.executeQuery();
            while (rs.next()){
                String field = rs.getString("Field");
                String type = rs.getString("Type");
                String key = rs.getString("Key");
                String aNull = rs.getString("Null");
                String aDefault = rs.getString("Default");
                String extra = rs.getString("Extra");
                filedEntities.add(new TableFiledEntity(field,type,aNull,key,aDefault,extra));
            }
            tableInfos.add(new TableInfo(tableName,filedEntities));
            closeStatement(st);
            closeResultSet(rs);
        }
        closeConnection(connection);
        closeStatement(statement);
        closeResultSet(resultSet);
        return tableInfos;
    }

    private String selectTableFieldSql(String tableName){
        return  "SHOW COLUMNS FROM " + tableName;
    }

    public void colseResource(Connection conn, Statement st, ResultSet rs) {
        closeResultSet(rs);
        closeStatement(st);
        closeConnection(conn);
    }


    public void closeConnection(Connection conn) {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            conn = null;
        }
    }

    public void closeStatement(Statement st) {
        if (st != null) {
            try {
                st.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            st = null;
        }
    }

    public void closeResultSet(ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            rs = null;
        }
    }


    public static void checkMysqlConnectorJar(){
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException("mysql-connector-java.jar no exist");
        }
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy