Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Get Column names using Metadata in jsp 
 

This section illustrates you how to get column names from the database using metadata in jsp.

 

Get Column names using Metadata in jsp

                         

This section illustrates you how to get column names from the database using metadata in jsp. 

Here we are providing you an example that retrieves all column names from a database table student1. Create a table student1 which contains student_id, student_name and student_address. With this example, we have to retrieve the column names using metadata. ResultSetMetaData provides methods that are used to get the characteristics of ResultSet such as number of columns, information about the columns i.e. datatype, length, scale, nullability. 

 

 

 

Table structure of student1

create table student ('student_id' int ,'student_name' 
varchar (256), 'student_address' varchar(256));

Here is the code of jspMetadata.jsp

<%@ page import="java.sql.*,java.io.*" %>
<html>
<head>
<title>Getting Column Names using Metadata</title>
</head>
<body>
<h2>Column Names of table "Student1"</h2>
<% 
Connection con=null;
ResultSet rs=null;
Statement stmt=null;
ResultSetMetaData md;
try{
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://192.168.10.59/anu?user=root&password=root";
con=DriverManager.getConnection(url);
stmt=con.createStatement();
}
catch(Exception e){
System.out.println(e.getMessage());
}
try{
stmt = con.createStatement();
rs = stmt.executeQuery("SELECT * FROM student1");
md = rs.getMetaData();
int count = md.getColumnCount();
out.println("<table border=1>");
out.print("<tr>");
for (int i=1; i<=count; i++) {
out.print("<th>");
out.print(md.getColumnName(i));
}
out.println("</tr>");
out.println("</table>");
}
catch (SQLException ex) {}
%>
</body>
</html>

Description of the code
In the above example, 
1) Import the packages java.sql.*, java.io.*
2) Load the database  driver which creates the connection with mysql database by Class.forName("com.mysql.jdbc.Driver") inside the try-catch.
3) Then create jdbc connection by con=DriverManager.getConnection(url). The URL consist of jdbc:mysql, host address, database, username and password of mysql like: jdbc:mysql://192.168.10.59/anu?user=root&password=root". 
4) After establishing the connection, create statement by using the method stmt=con.createStatement() to get the column names.
5) It will return the resultset which have the method stmt.executeQuery() to execute the statement.
6) ResultSet have the method getMetadata() which returns the metadata object and provides meta information of the resultset.
7) The method md.getColumnCount() of ResultSetMetadata object returns the number of columns for the resultset from the query:
    SELECT * FROM student1.
8) Then call the method md.getColumnName() which will return the column names from ResultSetMetadata object. 

Then the following output will be displayed.

Here is the output.

Download Source code

                         

» View all related tutorials
Related Tags: jsp div io sed vi rmi number visio opera ai js to division modulus e main use ul pe in

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.