Võ Văn Hải's blog

Chỉ có một điều tuyệt đối đó là mọi thứ đều tương đối…

Thao tác với Metadata

Lấy tất cả các bảng có trong 1 cơ sở dữ liệu

public Vector<String> getTable(){
		Vector<String> allField=new Vector<String>();
		try {	    
			String []tableTypes={"TABLE","VIEW"};
			ResultSet table=metadata.getTables(null,null,null,tableTypes);
			
			String TableName="";
			while(table.next()){
				TableName=table.getString("TABLE_NAME");
				allField.addElement(TableName);
			}
		}catch (Exception ex) {
			ex.printStackTrace();
	    }
		return allField;
	}

Lấy các fields có trong 1 bảng
public Vector<cRowInfos> getColumns(String tableName){
	Vector<cRowInfos> tblInfo=new Vector<cRowInfos>();
	try {
		ResultSet columnName=metadata.getColumns(null,null,tableName,null);
		while(columnName.next()){
			String n=(columnName.getString("COLUMN_NAME"));
			String t=""+(columnName.getString("TYPE_NAME"));
			String s=""+columnName.getString("COLUMN_SIZE");
			String nu=""+columnName.getString("IS_NULLABLE");
			tblInfo.addElement(new cRowInfos(n,t,s,nu));
		}
	}
	catch (Exception ex) {
		ex.printStackTrace();
	}
	return tblInfo;
}

Với cRowInfos có code như sau:
package vovanhai.wordpress.com.all;
public class cRowInfos
{
	protected String rName;
	protected String rLength;
	protected String rType;
	protected String rNullable;
	cRowInfos(String n,String t,String l,String na){
		rName=n;
		rLength=l;
		rType=t;
		rNullable=na;
	}
	public String toString(){
		return rName+";\t"+rType+";\t"+rLength+";\t"+rNullable;
	}
	public String getName(){
		return rName;
	}
	public String getLength(){
		return rLength;
	}
	public String getType(){
		return rType;
	}
	public String getNull(){
		return rNullable;
	}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

Join 230 other followers