Results 1 to 1 of 1
- 05-15-2011, 07:30 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 28
- Rep Power
- 0
How to arrange wideness of database columnns for a resultset ?
Hi, I ave a poroblem and could not find a solution on the internet yet.
I am using JDBC to get data from an access database. I use resultset and read funciton (that I got from internet) to view displayed results.
What I want to do is, see some columns not wide, and some columns wider.
For example, ID columsn which shows 2 digit numbers, should not be wide as others columns, and Address column should be wide.
Here is the code that shows resultset on the screen. Please Help
private void displayResultSet( ResultSet rs ) throws SQLException
{
// position to first record
boolean moreRecords = rs.next();
// If there are no records, display a message
if ( !moreRecords ) {
JOptionPane.showMessageDialog( this,
"ResultSet contained no records" );
setTitle( "No records to display" );
return;
}
Vector columnHeads = new Vector();
Vector rows = new Vector();
try {
// get column heads
ResultSetMetaData rsmd = rs.getMetaData();
for ( int i = 1; i <= rsmd.getColumnCount(); ++i )
columnHeads.addElement( rsmd.getColumnName( i ) );
// get row data
do {
rows.addElement( getNextRow( rs, rsmd ) );
} while ( rs.next() );
// display table with ResultSet contents
table = new JTable( rows, columnHeads );
JScrollPane scroller = new JScrollPane( table );
Container c = getContentPane();
c.remove( 1 );
c.add( scroller, BorderLayout.CENTER );
c.validate();
} // end try
catch ( SQLException sqlex ) {
sqlex.printStackTrace();
}
} // end method displayResultSet
private Vector getNextRow( ResultSet rs,
ResultSetMetaData rsmd ) throws SQLException
{
Vector currentRow = new Vector();
for ( int i = 1; i <= rsmd.getColumnCount(); ++i )
switch( rsmd.getColumnType( i ) ) {
case Types.VARCHAR:
case Types.LONGVARCHAR:
currentRow.addElement( rs.getString( i ) );
break;
case Types.INTEGER:
currentRow.addElement( new Long( rs.getLong( i ) ) );
break;
case Types.REAL:
currentRow.addElement( new Float( rs.getDouble( i ) ) );
break;
case Types.DATE:
currentRow.addElement( rs.getDate( i ) );
break;
default:
System.out.println( "Type was: " +
rsmd.getColumnTypeName( i ) );
}
return currentRow;
} // end method getNextRow
Similar Threads
-
Arrange the given numbers of the array in descending order
By radhi16 in forum New To JavaReplies: 3Last Post: 01-13-2011, 06:07 PM -
arrange(java.lang.String[][]) in Test cannot be applied to (java.lang.String) arrange
By prizzly in forum New To JavaReplies: 4Last Post: 01-01-2011, 10:52 AM -
Is resultset the mirror image of database table???
By Stephen Douglas in forum New To JavaReplies: 2Last Post: 04-10-2010, 03:01 PM -
Arrange components in a GridBagLayout
By ze snow in forum New To JavaReplies: 1Last Post: 02-27-2010, 02:22 PM -
Populating a combo-box in a JFrame with resultset from database
By matpj in forum AWT / SwingReplies: 11Last Post: 02-20-2009, 02:10 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks