| import java.sql.*; |
| import java.util.*; |
| |
| import com.mysql.jdbc.Connection; |
| import com.mysql.jdbc.PreparedStatement; |
| import com.mysql.jdbc.ResultSetMetaData; |
| |
| public class test { |
| private static final String DRIVER = "com.mysql.jdbc.Driver"; |
| private static final String URL = "jdbc:mysql://localhost:3306/world?useUnicode=true&characterEncoding=utf-8"; |
| private static final String USERNAME = "root"; |
| private static final String PASSWORD = "root"; |
| private static final String SQL = "SELECT * FROM "; |
| |
| static { |
| try { |
| Class.forName(DRIVER); |
| } catch (ClassNotFoundException e) { |
| } |
| } |
| |
| |
| |
| |
| |
| |
| public static Connection getConnection() { |
| Connection conn = null; |
| try { |
| conn = (Connection) DriverManager.getConnection(URL, USERNAME, PASSWORD); |
| } catch (SQLException e) { |
| |
| e.printStackTrace(); |
| } |
| return conn; |
| } |
| |
| |
| |
| |
| |
| public static void closeConnection(Connection conn) { |
| if(conn != null) { |
| try { |
| conn.close(); |
| } catch (SQLException e) { |
| e.printStackTrace(); |
| } |
| } |
| } |
| |
| |
| |
| |
| public static List<String> getTableNames() { |
| List<String> tableNames = new ArrayList<>(); |
| Connection conn = getConnection(); |
| ResultSet rs = null; |
| try { |
| |
| DatabaseMetaData db = conn.getMetaData(); |
| |
| rs = db.getTables(null, null, null, new String[] { "TABLE" }); |
| while(rs.next()) { |
| tableNames.add(rs.getString(3)); |
| } |
| } catch (SQLException e) { |
| e.printStackTrace(); |
| } finally { |
| try { |
| rs.close(); |
| closeConnection(conn); |
| } catch (SQLException e) { |
| e.printStackTrace(); |
| } |
| } |
| return tableNames; |
| } |
| |
| |
| |
| |
| |
| |
| public static List<String> getColumnNames(String tableName) { |
| List<String> columnNames = new ArrayList<>(); |
| |
| Connection conn = getConnection(); |
| PreparedStatement pStemt = null; |
| String tableSql = SQL + tableName; |
| try { |
| pStemt = (PreparedStatement) conn.prepareStatement(tableSql); |
| |
| ResultSetMetaData rsmd = (ResultSetMetaData) pStemt.getMetaData(); |
| |
| int size = rsmd.getColumnCount(); |
| for (int i = 0; i < size; i++) { |
| columnNames.add(rsmd.getColumnName(i + 1)); |
| } |
| } catch (SQLException e) { |
| e.printStackTrace(); |
| } finally { |
| if (pStemt != null) { |
| try { |
| pStemt.close(); |
| closeConnection(conn); |
| } catch (SQLException e) { |
| e.printStackTrace(); |
| } |
| } |
| } |
| return columnNames; |
| } |
| |
| |
| |
| |
| |
| |
| public static List<String> getColumnTypes(String tableName) { |
| List<String> columnTypes = new ArrayList<>(); |
| |
| Connection conn = getConnection(); |
| PreparedStatement pStemt = null; |
| String tableSql = SQL + tableName; |
| try { |
| pStemt = (PreparedStatement) conn.prepareStatement(tableSql); |
| |
| ResultSetMetaData rsmd = (ResultSetMetaData) pStemt.getMetaData(); |
| |
| int size = rsmd.getColumnCount(); |
| for (int i = 0; i < size; i++) { |
| columnTypes.add(rsmd.getColumnTypeName(i + 1)); |
| } |
| } catch (SQLException e) { |
| e.printStackTrace(); |
| } finally { |
| if (pStemt != null) { |
| try { |
| pStemt.close(); |
| closeConnection(conn); |
| } catch (SQLException e) { |
| e.printStackTrace(); |
| } |
| } |
| } |
| return columnTypes; |
| } |
| |
| |
| |
| |
| |
| |
| public static List<String> getColumnComments(String tableName) { |
| List<String> columnTypes = new ArrayList<>(); |
| |
| Connection conn = getConnection(); |
| PreparedStatement pStemt = null; |
| String tableSql = SQL + tableName; |
| List<String> columnComments = new ArrayList<>(); |
| ResultSet rs = null; |
| try { |
| pStemt = (PreparedStatement) conn.prepareStatement(tableSql); |
| rs = pStemt.executeQuery("show full columns from " + tableName); |
| while (rs.next()) { |
| columnComments.add(rs.getString("Comment")); |
| } |
| } catch (SQLException e) { |
| e.printStackTrace(); |
| e.printStackTrace(); |
| } finally { |
| if (rs != null) { |
| try { |
| rs.close(); |
| closeConnection(conn); |
| } catch (SQLException e) { |
| e.printStackTrace(); |
| } |
| } |
| } |
| return columnComments; |
| } |
| } |
| |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端