mySQL数据库文字提取_mysql数据库模式信息提取

此程序实现将mysql数据库的模式信息展示出来。下面我简要地说明一下。

1.树形结构的实现。

SimplePanel pl=new SimplePanel();

private void buildFrameLayout(){

try{ pane = getContentPane();

//关闭面板

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//BorderLayout布局管理器

JPanel panel = new JPanel();

final JScrollPane scrollPane = new JScrollPane();

//panel.setBounds(24, 10, 173, 317);

panel.add(scrollPane);

//初始化根节点

DefaultMutableTreeNode root=new DefaultMutableTreeNode(connection.getCatalog());

//获得表名

Vector vector = getTableNames();

DefaultMutableTreeNode menu_accp = null;

for(int i = 0; i < vector.size(); i++){

String strName = (String)vector.get(i);

//初始化枝节点

menu_accp = new DefaultMutableTreeNode(strName);

Vector vectorColumn = getColumnNames(strName);

DefaultMutableTreeNode menu_accp_son = null;

//添加枝节点数据

root.add(menu_accp);

for(int j = 0; j < vectorColumn.size(); j++){

//初始化叶子节点

menu_accp_son = new DefaultMutableTreeNode((String)vectorColumn.get(j));

//添加叶子节点数据

menu_accp.add(menu_accp_son);

}

}

tree = new JTree(root);

TreeSelectionModel model = tree.getSelectionModel();

//一次只能选择一个树节点

model.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);

//显示树

scrollPane.setViewportView(tree);

pane.add(panel,BorderLayout.WEST);

//注册点击节点事件

tree.addTreeSelectionListener(new TreeSelectionListener() {

public void valueChanged(TreeSelectionEvent e) {

DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree

.getLastSelectedPathComponent();

// 可选用户对象

Object nodeInfo = node.getUserObject();

if (node.getLevel()==0) {

//根节点不处理

} else if (node.getLevel()==1){

//枝节点

pl.getPanel(connection, nodeInfo.toString(),node.getParent().toString(),node.getLevel());

} else {

//叶子节点

pl.getPanel(connection, nodeInfo.toString(),node.getParent().toString(),node.getLevel());

}

}

});

pane.add(pl,BorderLayout.CENTER);

}catch(Exception e){e.printStackTrace();}

}

2.信息展示的实现。

public ColumnModel(Connection connection, String selectTable)

throws SQLException {

Statement stmt = connection.createStatement();

// 执行数据查找

ResultSet rset = stmt.executeQuery("SELECT * FROM " + selectTable);

// 结果集元数据

ResultSetMetaData rsmd = rset.getMetaData();

// 列长

int count = rsmd.getColumnCount();

String[] tableHeads = { "属性ID", "属性名" };

// 将表格头转换过向量类型,以备表格模型使用

columnHeaders = new Vector();

for (int i = 0; i < tableHeads.length; i++) {

columnHeaders.add(tableHeads[i]);

}

tableData = new Vector(count);

for (int i = 1; i <= count; i++) {

Vector rowData = new Vector(count);

rowData.add(i);

rowData.add(rsmd.getColumnName(i));

tableData.addElement(rowData);

}

}

public ColumnModel(Connection connection, String selectNode,

String parentNode) throws SQLException {

Statement stmt = connection.createStatement();

// 执行数据查找

ResultSet rset = stmt.executeQuery("SELECT * FROM " + parentNode);

// 结果集元数据

ResultSetMetaData rsmd = rset.getMetaData();

// 列长

int count = rsmd.getColumnCount();

String[] tableHeads = { "属性ID", "数据类型", "(最大)长度", "小数位" };

// 将表格头转换过向量类型,以备表格模型使用

columnHeaders = new Vector();

for (int i = 0; i < tableHeads.length; i++) {

columnHeaders.add(tableHeads[i]);

}

tableData = new Vector(count);

int strInt = 0;

for (int i = 1; i <= count; i++) {

if (rsmd.getColumnName(i).equals(selectNode)) {

strInt = i;

break;

}

}

Vector rowData = new Vector(count);

rowData.add(strInt);

rowData.add(rsmd.getColumnTypeName(strInt));

rowData.add(rsmd.getColumnDisplaySize(strInt));

rowData.add(rsmd.getPrecision(strInt));

tableData.addElement(rowData);

}

//=======================================================

public RowModel(Connection connection, String selectTable)

throws SQLException {

// 执行数据查找

DatabaseMetaData dbMetaData = connection.getMetaData();

// 结果集元数据

// ResultSetMetaData rsmd = rset.getMetaData();

// 获取表的主外键的描述

ResultSet tablePrimaryKeyInfo = dbMetaData.getPrimaryKeys(null,

null, selectTable);

// 列长

int count = 3;

String[] tableHeads = { "键名", "键属性(组)", "键类型" };

// 将表格头转换过向量类型,以备表格模型使用

columnHeaders = new Vector();

for (int i = 0; i < tableHeads.length; i++) {

columnHeaders.add(tableHeads[i]);

}

String strTest = "(";

String strTest1 = "";

while (tablePrimaryKeyInfo.next()) {

strTest += tablePrimaryKeyInfo.getString("COLUMN_NAME") + " ";

strTest1 = tablePrimaryKeyInfo.getString("PK_NAME");

}

strTest = strTest.trim().replace(' ', ',') + ")";

tableData = new Vector(count);

Vector rowData = new Vector(count);

rowData.add("PK_INTER");

rowData.add(strTest);

rowData.add(strTest1);

tableData.addElement(rowData);

}

public RowModel(Connection connection, String selectNode,

String parentNode) throws SQLException {

Statement stmt = connection.createStatement();

// 执行数据查找

ResultSet rset = stmt.executeQuery("SELECT * FROM " + parentNode);

// 结果集元数据

ResultSetMetaData rsmd = rset.getMetaData();

DatabaseMetaData dbMetaData = connection.getMetaData();

ResultSet columnInfo = dbMetaData.getColumns(null, null,

parentNode, selectNode);

// 列长

int count = rsmd.getColumnCount();

String[] tableHeads = { "属性ID", "DEFAULT", "NOT NULL" };

// 将表格头转换过向量类型,以备表格模型使用

columnHeaders = new Vector();

for (int i = 0; i < tableHeads.length; i++) {

columnHeaders.add(tableHeads[i]);

}

tableData = new Vector(count);

int strInt = 0;

for (int i = 1; i <= count; i++) {

if (rsmd.getColumnName(i).equals(selectNode)) {

strInt = i;

break;

}

}

while (columnInfo.next()) {

Vector rowData = new Vector(count);

rowData.add(strInt);

rowData.add(columnInfo.getString("COLUMN_DEF"));

rowData.add(columnInfo.getString("IS_NULLABLE"));

tableData.addElement(rowData);

}

}

//=======================================================

public ComponentModel(Connection connection, String selectTable)

throws SQLException {

// 执行数据查找

DatabaseMetaData dbMetaData = connection.getMetaData();

ResultSet tableForeignKeyInfo = dbMetaData.getImportedKeys(null,

null, selectTable);

String[] tableHeads = { "外键", "外键属性", "引用的关系", "引用的属性", "更新规则",

"删除规则" };

// 将表格头转换过向量类型,以备表格模型使用

columnHeaders = new Vector();

for (int i = 0; i < tableHeads.length; i++) {

columnHeaders.add(tableHeads[i]);

}

// 列长

int count = columnHeaders.size();

tableData = new Vector(count);

while (tableForeignKeyInfo.next()) {

Vector rowData = new Vector(count);

rowData.add("FK_ENTER");

rowData.add(tableForeignKeyInfo.getString("FKCOLUMN_NAME"));

rowData.add(tableForeignKeyInfo.getString("PKTABLE_NAME"));

rowData.add(tableForeignKeyInfo.getString("PKCOLUMN_NAME"));

short sht = Short.parseShort(tableForeignKeyInfo

.getString("UPDATE_RULE"));

String strTest = "";

switch (sht) {

// 如果已经被导入,则不允许更新主键

case 3:

strTest = "importedNoAction";

break;

// 将导入的键更改为与主键更新一致

case 0:

strTest = "importedKeyCascade";

break;

// 如果已更新导入键的主键,则将导入键更改为 NULL

case 2:

strTest = "importedKeySetNull";

break;

// 如果已更新导入键的主键,则将导入键更改为默认值

case 4:

strTest = "importedKeySetDefault";

break;

// 与 importedKeyNoAction 相同(为了与 ODBC 2.x 兼容)

case 1:

strTest = "importedKeyRestrict";

break;

default:

break;

}

rowData.add(strTest);

short sht1 = Short.parseShort(tableForeignKeyInfo

.getString("DELETE_RULE"));

String strTest1 = "";

switch (sht1) {

case 3:

strTest1 = "importedKeyNoAction";

break;

case 0:

strTest1 = "importedKeyCascade";

break;

case 2:

strTest1 = "importedKeySetNull";

break;

case 4:

strTest1 = "importedKeyRestrict";

break;

case 1:

strTest1 = "importedKeySetDefault";

break;

default:

break;

}

rowData.add(strTest1);

tableData.addElement(rowData);

}

}

public ComponentModel(Connection connection, String selectNode,

String parentNode) throws SQLException {

Statement stmt = connection.createStatement();

// 执行数据查找

ResultSet rset = stmt.executeQuery("SELECT * FROM " + parentNode);

// 结果集元数据

ResultSetMetaData rsmd = rset.getMetaData();

// 列长

int count = rsmd.getColumnCount();

String[] tableHeads = { "属性ID", "是否只读", "是否只写" };

// 将表格头转换过向量类型,以备表格模型使用

columnHeaders = new Vector();

for (int i = 0; i < tableHeads.length; i++) {

columnHeaders.add(tableHeads[i]);

}

tableData = new Vector(count);

int strInt = 0;

for (int i = 1; i <= count; i++) {

if (rsmd.getColumnName(i).equals(selectNode)) {

strInt = i;

break;

}

}

Vector rowData = new Vector(count);

rowData.add(strInt);

rowData.add(rsmd.isReadOnly(strInt));

rowData.add(rsmd.isWritable(strInt));

tableData.addElement(rowData);

}

最终的实现效果如下:

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值