import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;import junit.framework.TestCase;import utils.DBConnection;public class GetTales extends TestCase {    public void getTable() {
        // 1,建立Connection
        Connection conn = DBConnection.getConnection();
        ResultSet rs = null;
        // 2,获得DatabaseMetaData的实例
        DatabaseMetaData metaData = null;
        try {
            metaData = conn.getMetaData();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // 3,获取库名列表        try {
            rs = metaData.getCatalogs();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            while (rs.next()) {
                System.out.println(rs.getString("TABLE_CAT"));
                // System.out.println(rs.getString("lianxi_temp"));
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }        System.out.println("-------------------------");        // 4,获取表名列表
        ResultSet rs1 = null;
        try {
            rs1 = metaData.getTables(conn.getCatalog(), null, null, new String[] {"TABLE"});
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            while (rs1.next()) {
                System.out.println(rs.getString("TABLE_NAME"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
} ------------------------------------------------我这个只能把数据库名列表拿出来但是 表名列表为空

解决方案 »

  1.   

        DatabaseMetaData dbmd = connection.getMetaData();     // Specify the type of object; in this case we want tables
        String[] types = {"TABLE"}; 
        ResultSet resultSet = dbmd.getTables(null, null, "%", types); 
        
        // Get the table names 
        while (resultSet.next()) { 
    // Get the table name String 
    String tableName = resultSet.getString(3); 
    System.out.println("Table: " +tableName);
        } 
      

  2.   

    呵呵 DatabaseMetaData  就是这个
      

  3.   

    曾经写过oracle的和sql server的,没有直接到的列名,是通过sql查询获取
      

  4.   

    JDK中的ResultSetMetaData接口中有方法,你可以去看看