[JAVA100 example]057, result set meta data

Source: Internet
Author: User
Tags table name

Import java.sql.*;


/**


* <p>title: Result set meta data </p>


* <p>description: Obtain database information using the result set metadata object. </p>


* <p>copyright:copyright (c) 2003</p>


* <p>Filename:JDBCResultMeta.java</p>


* @version 1.0


*/


public class Jdbcresultmeta {


private String url= "";


private String username= "";


private String password= "";


/**


*<br> Method Description: Main method


*<br> input Parameters:


*<br> return type:


*/


public static void Main (java.lang.string[] args) {


if (args.length!=4) {


System.out.println ("Use:java jdbcresultmeta URL username password");


return;


  }


Jdbcresultmeta JRM = new Jdbcresultmeta ();


Jrm.url = Args[0];


Jrm.username=args[1];


jrm.password=args[2];


Jrm.getmeta (Jrm.conn (), args[3]);


}


/**


*<br> Method Description: Obtain data connection


*<br> input Parameters:


*<br> return Type: Connection Connection object


*/


public Connection Conn () {


try {


class.forname ("Oracle.jdbc.driver.OracleDriver");


Connection con = drivermanager.getconnection (URL, username, password);


return con;


}catch (ClassNotFoundException CF) {


System.out.println ("Can´t find Class" +CF);


return null;


}catch (SQLException sqle) {


System.out.println ("Can´t Connection db:" +sqle);


return null;


} catch (Exception e) {


System.out.println ("Failed to load Jdbc/odbc driver.");


return null;


  }


 }


/**


*<br> Method Description: Get result set metadata information


*<br> input parameters: Connection Con database connection


*<br> input parameter: String table name


*<br> return type:


*/


public void Getmeta (Connection con, String table) {


try {


Statement Stm = Con.createstatement ();


String sql= "select * from" +table;


ResultSet rs = stm.executequery (SQL);


ResultSetMetaData lineinfo = Rs.getmetadata ();


System.out.println ("*********************result META comment************************");


//Get the number of data columns


int columnCount = Lineinfo.getcolumncount ();


System.out.println ("Column Count:" +columncount);


//Get data column type


for (int i=1;i<columncount+1;i++) {


String columename = Lineinfo.getcolumnname (i);


String columetype = Lineinfo.getcolumntypename (i);


Boolean autocol = Lineinfo.isautoincrement (i);


System.out.println (columename+ "=" +columetype + ":::" +autocol);


   }


}catch (SQLException se) {


//Output database connection error message


System.out.println ("SQL Exception:" + se.getmessage ());


Se.printstacktrace (System.out);


}catch (Exception e) {


System.out.println (e);


}finally{


try{


Con.close ();


}catch (SQLException se) {}


  }


 }


}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.