Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML

Fresher Job


 

Search Host

Monthly Fee($)
Disk Space (MB)
Register With us for Newsletter!
Visit Forum! Post Questions!
Jobs At RoseIndia.net!

Have tutorials?
Add your tutorial to our Java Resource and get tons of hits.

We offer free hosting for your tutorials. and exposure for thousands of readers. drop a mail
roseindia_net@yahoo.com
 
   

Tutorials

Java Server Pages

JAXB

Java Beans

JDBC

MySQL

Java Servlets

Struts

Bioinformatics

Java Code Examples

Interview Questions

 
Join For Newsletter

Powered by groups.yahoo.com
Visit Group! Post Questions!

Web Promotion

Web Submission

Submit Sites

Manual Submission?

Web Promotion Guide

Hosting Companies

Web Hosting Guide

Web Hosting

Linux

Beginner Guide to Linux Server

Frameworks

Persistence Framework

Web Frameworks

Free EAI Tools

Web Servers

Aspect Oriented Programming

Free Proxy Servers

Softwares

Adware & Spyware Remover

Open Source Softwares

java
Expert:srikala
i need output as tablename;coloumn1;coloumn2,;coloumn3,fieldvalue1,fieldvalue2,and so on.
please tel me the javacode for this using resultset metadata
Answers
Hi friend,



1. First import the java packages

import java.sql.*;

2. Loading a database driver

String driver = "com.mysql.jdbc.Driver";

3.Creating a jdbc Connection

String url = "jdbc:mysql://localhost:3306/";;
String username = "root";
String password = "root";
String dbName= "dbname";
Class.forName(driver);
Connection conn = DriverManager.getConnection(url+dbName, username, password);

4. Creating a jdbc Statement object

Statement st = conn.createStatement();

5. Executing a statement with the Statement object, and returning a jdbc resultSet

ResultSet rs = st.executeQuery("SELECT * FROM tablename");

6. ResultSet has method getMetaData() which returns the ResultSet MetaData object which provides
meta information of the result set.

ResultSetMetaData rsmd = rs.getMetaData();

7. getColumnCount() method on ResultSetMetaData object returns the number of columns
for the result set returned from the query.

int totalCol=rsmd.getColumnCount();
System.out.println("Number of Columns="+totalCol);


8. getTableName() method on ResultSetMetaData object returns the name of table.


System.out.println("Table Name = " + rsmd.getTableName(int column));


9. getColumnName() method on ResultSetMetaData object returns the name of column table.
and getString(int column) method returns the value of column table.

while(rs.next())
{
for(int i=1;i<=totalCol;i++)
{
System.out.println("Column Name" +rsmd.getColumnName(i));
System.out.println("Field Value " + rs.getString(i));
}
}

For read more information :

http://www.roseindia.net/jdbc/get-column-count-using-result-set.shtml

Thanks.



More Questions
Post Answers
 
Ask Question Facing Programming Problem?
Useful Links
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

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

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

Copyright © 2007. All rights reserved.