ojdbc - how to see table structure in JDBC? -
i'm new jdbc,i've doubt,how see table structure using jdbc program..give code plese tried showing error code
import java.sql.*; public class ram { static final string jdbc_driver = "oracle.jdbc.driver.oracledriver"; static final string db_url = "jdbc:oracle:thin:@192.168.1.12:1521:aftdb"; static final string user = "system"; static final string pass = "manager"; public static void main(string[] args) { connection conn = null; statement stmt = null; try{ class.forname("oracle.jdbc.driver.oracledriver"); system.out.println("connecting database..."); conn = drivermanager.getconnection(db_url,user,pass); system.out.println("creating statement..."); stmt = conn.createstatement(); string sql; sql = "desc emp"; resultset rs = stmt.executequery(sql); while(rs.next()){ string emp= rs.getstring("emp"); system.out.println("id: " + emp); } rs.close(); stmt.close(); conn.close(); }catch(sqlexception se){ se.printstacktrace(); }catch(exception e){ e.printstacktrace(); }finally{ try{ if(stmt!=null) stmt.close(); }catch(sqlexception se2){ }// nothing can try{ if(conn!=null) conn.close(); }catch(sqlexception se){ se.printstacktrace(); } } } }
but showing error.please 1 how table structure in jdbc program,suggest me
documentation resultsetmetadata
use resultsetmetadata
change sql query
sql = "desc emp";
as
sql = "select * emp";
for example documentation
resultset rs = stmt.executequery("select * emp"); resultsetmetadata rsmd = rs.getmetadata(); system.out.println("no. of columns : " + rsmd.getcolumncount()); system.out.println("column name of 1st column : " + rsmd.getcolumnname(1)); system.out.println("column type of 1st column : " + rsmd.getcolumntypename(1));
Comments
Post a Comment