<file_sep>package pri.liyang.test;

import pri.liyang.service.MySQLService;

import java.sql.SQLException;

public class TestConnection {

    public static void main(String[] args) throws SQLException {

        System.out.println(new MySQLService().getAllTableName());

    }

}
<file_sep>package pri.liyang.interfaces;

import pri.liyang.entity.FieldInfo;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * 数据库业务接口
 */
public interface DBService {

    /**
     * 获取当前数据库的全部表名
     * @return List<String>表名
     * @throws SQLException
     */
    List<String> getAllTableName() throws SQLException;

    /**
     * 根据表名，获取该表的表结构信息
     * @param tableName
     * @return
     * @throws SQLException
     */
    List getFieldInfoListByTableName(String tableName) throws SQLException;

}
<file_sep>package pri.liyang.handler;

import pri.liyang.interfaces.ConnectHandler;
import pri.liyang.system.SystemConfig;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * MySQL的连接处理类
 */
public class MySQLConnectHandler implements ConnectHandler {

    /**
     * 单例Connection
     */
    private static Connection connection = null;

    /**
     * 初始化连接
     */
    static{
        try {
            //从配置文件获取数据库连接参数
            String driver = SystemConfig.getSystemProperty("driver");
            String url = SystemConfig.getSystemProperty("url");
            String username = SystemConfig.getSystemProperty("username");
            String password = SystemConfig.getSystemProperty("password");

            //注册Driver
            Class.forName(driver);

            //获取Connection
            connection = DriverManager.getConnection(url, username, password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取连接对象
     * @return Connection
     */
    @Override
    public Connection getConnection(){
        return connection;
    }

}
<file_sep>package pri.liyang.interfaces;

import java.sql.Connection;

/**
 * 数据库连接处理接口
 */
public interface ConnectHandler {

    /**
     * 获取数据库连接
     * @return
     */
    Connection getConnection();

}
