Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Guys and Girls use this cool program for output

 
Amit Barot
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
package jdbcpackage;
import java.sql.*;
public class DataFormating{
static ResultSetMetaData metadata;
static int columnCount;
// Format any ResultSet with this methods //
// ResultSet st is the ResultSet object in your class,RSMD is the RSMD object in your class
// int num is the numbers of columns in your ResultSet.
publicstatic void formatData(ResultSet st,ResultSetMetaData rsmd)throws SQLException{
metadata = rsmd;
columnCount = rsmd.getColumnCount();
for(int columns=1;columns<=columnCount;columns++){
String rowData = st.getString(columns);
String title = rsmd.getColumnName(columns);
System.out.print(rowData);
int j = 0;
while(j<((title.length())-(rowData.length())+20)){
System.out.print(" ");
j++;
}
}
System.out.println();
}
// Format any ResultSet with this methods //
// ResutlSetMetaData is the RSMD object in your class.
// int num is the numbers of columns in your ResultSet.
// For parting the Title from the Data columns.
publicstaticvoid formatTitle(ResultSetMetaData rsmd)throws SQLException{
columnCount = rsmd.getColumnCount();
for(int columns=1;columns<=columnCount;columns++){
System.out.print(rsmd.getColumnName(columns));
int j = 0;
while(j<20){
System.out.print(" ");
j++;
}
}System.out.println();
int k=0;
while(k<=getNameLength(rsmd)){
System.out.print( "-");
k++;
}
System.out.println();
}
public static int getNameLength(ResultSetMetaData rsmd) throws SQLException{
int len=0;
for(int i = 0;i<rsmd.getColumnCount();i++){>
len += rsmd.getColumnName(i+1).length();
}
return (len+((rsmd.getColumnCount()-1)*20))-1;
}
}
/*
eg: How to use this program
Resultset_object = Statement_object.createStatement();
Resultset_object = statement.executeQuery("select * from emp_table");
DataFormating.formatTitle(ResultSet set);
DataFormating.formatData(ResultSet set,ResultSetMetaData rsmd);
*/
Hope u get it and use it try it seriously it is a very good program and let me know how u find it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic