Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Still having problems obtaining Column Names

843859Jun 7 2010 — edited Nov 20 2014
Hi all,

I am still having problems displaying the column names from my database. I corrected most of my earlier mistakes of having the result set, and result set meta data as class variables. They are now local variables but still I get the same result, the odd thing is though I get the database information to display but not the column names. Here is my code:
protected Vector<String> initializeDB(){

		Vector <String> row;
		Vector <String> rowData;
		Vector <String> colNames;
		ResultSet rs = null;
		Statement sm = null;
		ResultSetMetaData rsmd;
		try{
			if(connected){
				sm = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
				rs = sm.executeQuery("select * from employee");
				rsmd = rs.getMetaData();
				numberOfColumns = rsmd.getColumnCount();
				colNames = new Vector<String>(numberOfColumns);
				for(int i = 1; i <= numberOfColumns; i++){
					colNames.add(rsmd.getColumnName(i));
				}
				setColumnNames(colNames);
				row = new Vector <String> ();
				while(rs.next()){
					rowData = new Vector<String>(numberOfColumns);
					for(int j = 1; j <= numberOfColumns; j++){
						rowData.add(rs.getString(j));
					}
					row.addAll(rowData);
				}
				return row;
			}
		}catch(SQLException se){
			displayErrors(se);
		}
		return null;
	}
This method just does a basic select all so as to initialize a table. Right now I'm simply trying to have the column names and rows display on the console to check if everything is working. The data displays fine but the column names just display null. Here is my getter and setters for the column names:
private void setColumnNames(Vector <String> colNames){
		this.columnNames = colNames;
	}
	
public Vector <String> getColumnNames(){
	        return this.columnNames;
	}
Finally, here is my testing code:
public class TestSQLUtilities {
	
	static HRES_SQL_Utilities sql;
	static Vector <String> rowInfo;
	static Vector <String> colNames;
	static String results;
	
	public static void main(String [] args){
		sql = new HRES_SQL_Utilities();
		rowInfo = new Vector <String>();
		results = new String();
		
		sql.connect_To_Database("jdbc:mysql:///hres_employee_test", "test", "devika8873");
		if(sql.connectionEstablished() == true){
			System.out.println("We are connected to the DB");
		}else{
			System.out.println("We are not connected to the DB");
		}
		colNames = sql.getColumnNames();
		rowInfo = sql.initializeDB();
		
		System.out.println(colNames);
		System.out.println(rowInfo);
		
	}

}
Again the rowInfo prints fine but the colNames just says null. Any help on this would be appreciated I was thinking the Vector's addAll method but since the result set meta data returns a String instead of a collection that won't help. Thanks again for any help.

Regards,
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 6 2010
Added on Jun 7 2010
9 comments
193 views