Mysql数据库查询表以及表中的字段名和类型

<br>import org.springframework.stereotype.Service;<br>import sailingdscg_ws.sailingdscg_ws.entity.TableField;<br>import sailingdscg_ws.sailingdscg_ws.service.IOracleTablesUtils;<br><br>import javax.servlet.ServletException;<br>import java.io.IOException;<br>import java.sql.*;<br>import java.util.ArrayList;<br>import java.util.HashMap;<br>import java.util.List;<br>import java.util.Map;<br><br>@Service<br>public class OracleTablesUtils implements IOracleTablesUtils {<br><br>    private static final long serialVersionUID = 1L;<br>    Connection conn = null;<br>    Statement st = null;<br><br><br>    //获取conn<br>    @Override<br>    public Boolean init(String url, String username, String password) throws ServletException {<br>        try {<br>            Class.forName("com.mysql.jdbc.Driver").newInstance();<br>            conn = java.sql.DriverManager.getConnection(url, username, password);<br>        } catch (InstantiationException e) {<br>            e.printStackTrace();<br>        } catch (IllegalAccessException e) {<br>            e.printStackTrace();<br>        } catch (ClassNotFoundException e) {<br>            e.printStackTrace();<br>        } catch (SQLException e) {<br>            e.printStackTrace();<br>        }<br>        if (conn != null) {<br>            return true;<br>        } else {<br>            return false;<br>        }<br>    }<br><br>    @Override<br>    public Map<String, List<TableField>> getTables() throws ServletException, IOException {<br>        // 1、获取数据库所有表<br>        List<String> tables = new ArrayList<String>();<br>        try {<br>            DatabaseMetaData dbMetaData = conn.getMetaData();<br>            ResultSet rs = dbMetaData.getTables(null, null, null, new String[]{"TABLE"});<br>            while (rs.next()) {<br>                tables.add(rs.getString("TABLE_NAME"));<br>            }<br>        } catch (SQLException e) {<br>            e.printStackTrace();<br>        }<br>        // 2、遍历数据库表,获取各表的字段等信息<br>        Map<String, List<TableField>> map = new HashMap<>();<br>        for (String tableName : tables) {<br>            List<TableField> tablesMessage = new ArrayList<TableField>();<br>            String sql = "select * from " + tableName;<br>            try {<br>                PreparedStatement ps = conn.prepareStatement(sql);<br>                ResultSet rs = ps.executeQuery();<br>                ResultSetMetaData meta = rs.getMetaData();<br>                int columeCount = meta.getColumnCount();<br>                for (int i = 1; i < columeCount + 1; i++) {<br>                    TableField tableField = new TableField();<br>                    tableField.setName(meta.getColumnName(i));<br>                    tableField.setType(Integer.toString(meta.getColumnType(i)));<br>                    tablesMessage.add(tableField);<br>                }<br>                map.put(tableName, tablesMessage);<br>            } catch (SQLException e) {<br>                e.printStackTrace();<br>            }<br>        }<br>        return map;<br>    }<br><br>    //释放conn<br>    @Override<br>    public void destroy() {<br>        if (conn != null) {<br>            try {<br>                conn.close();<br>            } catch (SQLException e) {<br>                e.printStackTrace();<br>            }<br>        }<br>    }<br>}