We help IT Professionals succeed at work.

retrive data FROM DATABASE AND READ DATA IN TXT FILE

kalyaniramana
on
DEAR SIR/MEDAM

I AM RETRIEVING DATA FROM THE DATABASE.

I WANT READ(SEE) THE DATA IN  NOTEPAD(A.TXT).

PLS ANY ONE KNOWS HOW TO  READ DATA IN TXT FILES USING SERVLETS.

in notepad the data should display the  orginal lengh of the data type.data should be in order

PLEASE IF ANY ONE KNOWS THIS SEND ME CODE.


Comment
Watch Question


import java.sql.*;
import java.util.*;
         
public class DatabaseTableViewer {
       BufferedWriter out = new BufferedWriter(
                                new FileWriter(FILENAME));

          // execute select query
         String query = "SELECT * FROM " + TABLE_NAME + ";";  
         ResultSet table = stmt.executeQuery(query);
           
         // determine properties of table
         ResultSetMetaData meta = table.getMetaData();
         String[] colNames = new String[meta.getColumnCount()];
         Vector[] cells = new Vector[colNames.length];
         for( int col = 0; col < colNames.length; col++) {
            colNames[col] = meta.getColumnName(col + 1);
            cells[col] = new Vector();
         }

         // hold data from result set
         while(table.next()) {
            for(int col = 0; col < colNames.length; col++) {
               Object cell = table.getObject(colNames[col]);
               cells[col].add(cell);
            }
         }
         
         // print column headings
         for(int col = 0; col < colNames.length; col++)
            out.write(colNames[col].toUpperCase() + "\t" );
         out.newLine();

         // print data row-wise
         while(!cells[0].isEmpty()) {
            for(int col = 0; col < colNames.length; col++)
               out.write(cells[col].remove(0).toString()
                                   + "\t");
            out.newLine();
         }
          out.close();

      }

      // exit more gently
      catch(Exception e) {
         e.printStackTrace();
      }
   }
}

       
This code is to give a general idea .NOT Tested


These links might help

http://www.javaworld.com/javaworld/javatips/jw-javatip82.html

http://www.acm.org/crossroads/columns/ovp/march2001.htm


hope this helps
Did this work for you

just curious?

Author

Commented:
thanks sanjay
Hi ,

If the code has worked for you
accept it as an answer so that
the person posting it gets his/her points

Thanks

Author

Commented:
hi
this code is working,

in output data shoud displaying inproper.

i want to display the exact same lengh i specified in table.

in table
eno- varchar(10)
ename-varchar(12)
output i want

eno          ename
10--------   sham--------

it means not dashes , iwant spaces


thanks in advance
Hi,

I think you can perform some string manipulation
to replace
dash with spaces


   while(!cells[0].isEmpty()) {
           for(int col = 0; col < colNames.length; col++)
              //out.write(cells[col].remove(0).toString()
              //                    + "\t");

            String s = replaceChar( cells[col].remove(0).toString());
            out.write(s + "\t")
           out.newLine();
        }





//if s contains dashes  just replace
//there can be better ways to do this
//I am just trying to give you one approach
//you can optimize this code

 public String replaceChar(String s)
  {
  char repC  = '-';
  char withC = ' ';

  String k = s.substring( 0 , s.indexOf("-") );
  String g = s.substring( s.indexOf("-") , s.lastIndexOf("-") );  
  String f =g.replace(repC , withC);  
  String finalStr = k + f;
  System.out.println("val" + finalStr);
  return finalStr;


  }


hope this helps

Author

Commented:
dashes means i am  not getting any dashes, just i put like that.

when i am using privious code, i am getting this output.but i want not like this

NAME     NUMBER     OLDIC     NEWIC     SAL    
ramana     222     333     444     55555    
satish     777     666     5555     4444    
shakar nekkanti     1234567890     a3784756f     578-66706-7     32,900    
shakar nekka     1234567890           578-66706-7     32,900


i want like this

name                  number
ramana                   22


okk,

In that case print the data column wise

make the all the strings in each column of the same length

before printing and data implement an algorithm
like this


for each column find the length of the longest string
(including the header)

once you have the longest string
create all the other strings (from the column) of the same length
by adding spaces.

print the values in the column

I dont have a database setup to test the code
but I am pretty sure this will work




Author

Commented:
pls send code.i want display each cloumn length like database field length.

thanks

Commented:
This question appears to have been abandoned. A Moderator will be asked to
close this question after seven days, with the following recommended
disposition:

Points to sanjay_thakur

If you have any comment or objection to the recommendation, please leave it here.

Please do NOT accept this comment as an answer. Thank You!

guidway :0)
EE Cleanup Volunteer
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange

Explore More ContentExplore courses, solutions, and other research materials related to this topic.