tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Loïc Lefèvre <llefe...@fivia.com>
Subject RE: How can i get all table's name from JDBC driver?
Date Mon, 23 Jul 2001 15:11:01 GMT
Here is some source code in java:
usage:

	java <parameters> testOra url:port user password <file with one request per
line> alltables

If the connection fail, try to change the jdbc driver...

---8<-----------------------------------------------------------------------
-----------

import java.io.*;
import java.net.*;
import java.util.*;
import java.sql.*;


/**
 *
 */
class testOra
{

    static ArrayList tables = new ArrayList();
	static int n = 0;

	/**
	 *
	 */
	public static void main (String[] argv)
	{

		try
		{
			Connection con;

			DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
			con = DriverManager.getConnection ("jdbc:oracle:thin:@" + argv[0],
argv[1], argv[2]);

			System.out.println("Connection OK !");

			Statement stmt = con.createStatement();

            BufferedReader input = new BufferedReader(new
FileReader(argv[3]));
            String line = "";

            if(argv.length > 4)
	    	{
	    	    DatabaseMetaData dbmd = con.getMetaData();
                String [] types = new String[7];
                types[0] = "TABLE";
                types[1] = "VIEW";
                types[2] = "SYSTEM TABLE";
                types[3] = "GLOBAL TEMPORARY";
                types[4] = "LOCAL TEMPORARY";
                types[5] = "ALIAS";
                types[6] = "SYNONYM";
                ResultSet rset = dbmd.getTables("%","%","%",types);


System.out.println("--------------------------------------------------------
-----------------------------------------");
  		        System.out.println("DATABASE'S TABLES");

   			    execRequest(rset);
            }

    		int num = 1;
	    	while((line=input.readLine()) != null)
		    {

System.out.println("--------------------------------------------------------
-----------------------------------------");
    		    System.out.println("REQUETE numero : "+num);
			    num++;

    			ResultSet rs = stmt.executeQuery(line);
    			execRequest(rs);
            }


System.out.println("--------------------------------------------------------
-----------------------------------------");

	    	if(argv.length > 4)
	    	{
	    	    Iterator i = tables.iterator();
	    	    while(i.hasNext())
		        {
                    line = (String)i.next();

System.out.println("--------------------------------------------------------
-----------------------------------------");
    		        System.out.println("TABLE : "+line);
    		        System.err.println("Processing table "+line+"...");

    		    	try
    		    	{
    		    	    ResultSet rs = stmt.executeQuery("select * from "+line);
        	    		execRequest(rs);
    		    	}
                    catch(Exception e)
                    {
                        System.out.println("Impossible d'executer : select *
from "+line);
                        System.out.println(e.getMessage());
                    }
                }


System.out.println("--------------------------------------------------------
-----------------------------------------");
            }
	    }
	    catch (Exception e)
	    {
			e.printStackTrace();
		}
	}

    public static void execRequest(ResultSet rs)
    throws Exception
    {
		ResultSetMetaData rsmd = rs.getMetaData();

		String labels = "";
		for(int i = 1; i <= rsmd.getColumnCount(); i++ )
    	{
		    labels+=rsmd.getColumnName(i)+"\t";
		}

        System.out.println(labels);

		String	r1,r2,r3,r4,r5,r6,r7,r8,r9,r10;
		String	r11,r12,r13,r14,r15,r16,r17,r18,r19,r20;
		String	r21,r22,r23,r24,r25,r26,r27,r28,r29,r30;
		String	r31,r32,r33,r34,r35,r36,r37,r38,r39,r40;

		while( rs.next() )
		{
			r1 = r2 = r3 = r4 = r5 = r6 = r7 = r8 = r9 = r10 = "";
			r11 = r12 = r13 = r14 = r15 = r16 = r17 = r18 = r19 = r20 = "";
			r21 = r22 = r23 = r24 = r25 = r26 = r27 = r28 = r29 = r30 = "";
			r31 = r32 = r33 = r34 = r35 = r36 = r37 = r38 = r39 = r40 = "";
			try
			{
				r1 = rs.getString(1);
				r2 = rs.getString(2);
				r3 = rs.getString(3);
				r4 = rs.getString(4);
				r5 = rs.getString(5);
				r6 = rs.getString(6);
				r7 = rs.getString(7);
				r8 = rs.getString(8);
				r9 = rs.getString(9);
				r10 = rs.getString(10);
				r11 = rs.getString(11);
				r12 = rs.getString(12);
				r13 = rs.getString(13);
				r14 = rs.getString(14);
				r15 = rs.getString(15);
				r16 = rs.getString(16);
				r17 = rs.getString(17);
				r18 = rs.getString(18);
				r19 = rs.getString(19);
				r20 = rs.getString(20);
				r21 = rs.getString(21);
				r22 = rs.getString(22);
				r23 = rs.getString(23);
				r24 = rs.getString(24);
				r25 = rs.getString(25);
				r26 = rs.getString(26);
				r27 = rs.getString(27);
				r28 = rs.getString(28);
				r29 = rs.getString(29);
				r30 = rs.getString(30);
				r31 = rs.getString(21);
				r32 = rs.getString(22);
				r33 = rs.getString(23);
				r34 = rs.getString(24);
				r35 = rs.getString(25);
				r36 = rs.getString(26);
				r37 = rs.getString(27);
				r38 = rs.getString(28);
				r39 = rs.getString(29);
				r40 = rs.getString(30);
			}
			catch (Exception n)
			{}

			if( n == 0 )
		        tables.add(r3);

      		System.out.println(r1 + "\t" + r2 + "\t" + r3 + "\t" + r4 + "\t" +
r5 + "\t" + r6 + "\t" + r7 + "\t" + r8 + "\t" + r9 + "\t" + r10 + "\t" + r11
+ "\t" + r12 + "\t" + r13 + "\t" + r14 + "\t" + r15 + "\t" + r16 + "\t" +
r17 + "\t" + r18 + "\t" + r19 + "\t" + r20 + "\t" + r21 + "\t" + r22 + "\t"
+ r23 + "\t" + r24 + "\t" + r25 + "\t" + r26 + "\t" + r27 + "\t" + r28 +
"\t" + r29 + "\t" + r30  + "\t" + r31 + "\t" + r32 + "\t" + r33 + "\t" + r34
+ "\t" + r35 + "\t" + r36 + "\t" + r37 + "\t" + r38 + "\t" + r39 + "\t" +
r40);
      	}
      	n++;
    }
}

---8<-----------------------------------------------------------------------
---------------------

Loïc Lefèvre

-----Message d'origine-----
De : João Folha [mailto:joao.folha@lis2.siemens.pt]
Envoyé : mardi 24 juillet 2001 04:48
À : tomcat-user@jakarta.apache.org
Objet : How can i get all table's name from JDBC driver?


Hi there,

I need to get all table's name from JDBC driver and all column's name from
any table.

Can you help me?

cheers
João Folha


Mime
View raw message