ถ้าเราไม่รู้ว่าใน Table มี ฟิวล์อะไรบ้าง คุณสามารถนำวิธีนี้ไปใช้หาข้อมูลใน Table นั้นได้ครับ
*********************************************************************
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
public class ColumnName {
public static void main(String[] args) throws Exception {
System.out.println("Getting Column Names ");
Connection con = null;
String url = "jdbc:oracle:thin:@111.11.11.14:1521:TEST01";
String driver = "oracle.jdbc.driver.OracleDriver";
String user = "user11";
String pass = "pass11";
try{
Class.forName(driver);
con = DriverManager.getConnection(url, user, pass);
try{
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM billing_account");
ResultSetMetaData md = rs.getMetaData();
int col = md.getColumnCount();
System.out.println("Number of Column : "+ col);
System.out.println("Columns Name: ");
for (int i = 1; i <= col; i++){
String colName = md.getColumnName(i);
String colType = md.getColumnTypeName(i);
int displaySize = md.getColumnDisplaySize(i);
System.out.println(colName+" "+colType+" "+displaySize);
}
}
catch (SQLException s){
System.out.println("SQL statement is not executed!");
}
}
catch (Exception e){
e.printStackTrace();
}finally{
System.out.println(con.isClosed());
if(con!=null){
con.close();
}
}
}
}
********** Output *******************
Getting Column Names
Number of Column : 175
Columns Name:
BAN NUMBER 21
SYS_CREATION_DATE DATE 7
SYS_UPDATE_DATE DATE 7
OPERATOR_ID NUMBER 21
APPLICATION_ID CHAR 6
DL_SERVICE_CODE CHAR 5
DL_UPDATE_STAMP NUMBER 21
ACCOUNT_TYPE CHAR 3
ACCOUNT_CATEGORY CHAR 1
CUSTOMER_ID NUMBER 21
BAN_STATUS CHAR 1
DEFAULT_BEN NUMBER 21
START_SERVICE_DATE DATE 7
TAOFP_IND CHAR 1
FIRST_TOPUP_IND CHAR 1
FP_PASSWORD VARCHAR2 30
false