从oracle db

时间:2015-08-05 06:49:18

标签: java jtree

package com.view.custom;

import com.mysql.jdbc.DatabaseMetaData;
import com.mysql.jdbc.ResultSetMetaData;
import com.util.DBConnection;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;

public class ReadData {
    public static String getTableData(Connection con) {
        String col_attr1 = "";
        try {
            ArrayList tablename = new ArrayList();
            Statement stt = con.createStatement();
            String database = "Source Tables";

            ResultSet rs = null;
            DatabaseMetaData meta = (DatabaseMetaData) con.getMetaData();
            rs = meta.getTables(null, null, null, new String[] { "TABLE" });
            while (rs.next()) {
                String ab = rs.getString("TABLE_NAME");
                System.out.println(ab);
                tablename.add(ab);
            }

            for (Object tablename1 : tablename) {
                String sql = "Select * from " + tablename1.toString();
                String table_name = tablename1.toString();
                System.out.println("Table name " + table_name);

                ResultSet rs1 = stt.executeQuery(sql);
                ResultSetMetaData metadata = (ResultSetMetaData) rs1.getMetaData();
                int cou = metadata.getColumnCount();
                String col_attr = "";
                String profile1 = "";
                for (int j = 1; j <= cou; j++) {
                    String ab = metadata.getColumnName(j);
                    for (int c = j; c <= cou; c++) {
                        String val = "";
                        while (rs1.next()) {
                            val += rs1.getString(c) + "$";
                        }
                        rs1.beforeFirst();
                        if (val.length() > 1)
                            val = val.substring(0, val.lastIndexOf("$"));
                        profile1 += ab + "#" + val + "|";
                        break;
                    }
                }

                profile1 = profile1.substring(0, profile1.lastIndexOf("|"));
                col_attr1 += table_name + "@" + profile1 + "\n";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return col_attr1;
    }

    public static JTree getTree(String str, String rootName) {
        str = str.substring(0, str.lastIndexOf("\n"));
        String args[] = str.split("\n");

        DefaultMutableTreeNode dt = new DefaultMutableTreeNode(rootName);
        for (String arg : args) {
            String[] aa = arg.split("@"); // table name
            String bb[] = aa[1].split("\\|");
            System.out.println("table lenght: " + aa.length);

            DefaultMutableTreeNode dm = new DefaultMutableTreeNode(aa[0]);
            System.out.println("table : " + aa[0]);
            dt.add(dm);
            for (String bb1 : bb) {
                String[] dd = bb1.split("#"); // col value

                DefaultMutableTreeNode dm1 = new DefaultMutableTreeNode(dd[0]);
                dm.add(dm1);
                if (dd.length > 1) {
                    String dd1[] = dd[1].split("\\$");
                    for (String dd11 : dd1) {
                        DefaultMutableTreeNode dm2 = new DefaultMutableTreeNode(
                                dd11);
                        dm1.add(dm2);
                    }
                }
            }
        }

        return new JTree(dt);
    }
}

它没有取树。

0 个答案:

没有答案