Same old...number of rows in a resultset...but a different question

Hi,

I'm writing a code to generate report from the database. Everything is working absolutely fine. Now the problem is that when I try to check whether any row has been returned, I'm not able to do it correctly. What is happening is that the column heading is also being considered as a data and being written to the output file. I want that if there is no data ( excepting the column headings ) the program should terminate. Can anyone help me out of this? Thanks & regards,

Faisal

[508 byte] By [faisal.chawdharya] at [2007-10-2 23:22:52]
# 1

You need to provide more information.

A standard JDBC driver will not due that unless on of the following is true.

1. You are explicitly extracting the meta data (headers) from it.

2. You are doing SQL that returns that information as data.

You could be using a driver that isn't standard. Or you could be using something besides JDBC (which this forum is for.)

jschella at 2007-7-14 16:01:10 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2

Hi,

Plz find the code below :

import java.io.*;

import java.sql.*;

import java.util.*;

public class JDBC8

{

public static void main (String[] args) throws Exception

{

Connection con = null;

final String query = "select TNAME,TABTYPE from tab where tname like '%FAZ%'";

Statement stmt = null;

int ctr=0;

int maxColLength = 0;

String fileName = "";

ResultSet rs = null;

FileNameCreater obj = new FileNameCreater();

fileName = obj.getFileName();

int t = 0;

System.out.println("Created file : " +fileName);

try {

// load the driver

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

}

catch (java.lang.ClassNotFoundException e)

{

System.err.print("ClassNotFoundException.");

System.err.println(e.getMessage());

}

System.out.println("Connecting to the database. Please wait....");

try {

// define the connection URL. establish the connection.

con = DriverManager.getConnection("jdbc:odbc:DSN1","mis","abcdef#0");

if ( con.isClosed() )

{

System.out.println("Failed to connect to the database. Press enter to continue.");

System.in.read();

}

else

{

System.out.println("Connected to the database.");

}

System.out.println("Processing...Please be patient...");

// create a statement object

stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);

// Execute a query

rs = stmt.executeQuery(query);

ResultSetMetaData rsmd = rs.getMetaData();

rs.first();

System.out.println("check 1 = " + rs.getRow());

System.in.read();

int cntr = 0;

String tempStr = "";

String tempLbl = "";

boolean flag = false;

boolean inWhile = false;

rs.beforeFirst();

while ( rs.next() )

{

System.out.println("entered while");

System.in.read();

tempStr = rs.getString(++cntr);

tempLbl = rsmd.getColumnLabel(cntr);

System.out.println("cntr = " + cntr + " tempStr = " + tempStr + " tempLbl = " + tempLbl);

if ( tempStr == tempLbl )

flag = true;

else

flag = false;

}

inWhile = true;

if ( inWhile )

{

rs.beforeFirst();

FileWriter writer = new FileWriter(fileName);

BufferedWriter buf_Writer = new BufferedWriter(writer);

System.out.println("Processing started...");

int colCount = 0;

colCount = rsmd.getColumnCount();

String colLabel = "";

for ( int x = 1; x <= colCount; x++ )

{

colLabel = rsmd.getColumnLabel(x);

buf_Writer.write(colLabel);

if ( x < colCount )

buf_Writer.write("|");

}

buf_Writer.newLine();

rs.beforeFirst();

// Process the results

while(rs.next())

{

ctr++;

for (int i = 1; i <= colCount; i++)

{

String columnValue = rs.getString(i);

buf_Writer.write(columnValue);

if ( i < colCount )

buf_Writer.write("|");

}

buf_Writer.newLine();

}

buf_Writer.close();

}

}

catch (SQLException e)

{

System.err.println("SQLException : " + e.getMessage());

}

finally

{

try

{

// Close the resultset, statement and connection objects.

if (rs != null)

rs.close();

if (stmt != null)

stmt.close();

if (con!=null)

con.close();

}

catch(Exception ex)

{

System.out.println("Exception encountered: "+ ex.getMessage());

}

}

System.out.println("Program completed successfully.");

} // end of main

} // end of class

faisal.chawdharya at 2007-7-14 16:01:10 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3
Use code tags when you post code.> ResultSetMetaData rsmd = rs.getMetaData();Meta data is the names of columns. I didn't look through your code but if you don't want the column names then you shouldn't be using this.
jschella at 2007-7-14 16:01:10 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...