populatin jComBobox with data from a db tbl(table) column
Posted
by mnmyles
on Stack Overflow
See other posts from Stack Overflow
or by mnmyles
Published on 2010-04-21T06:57:51Z
Indexed on
2010/04/21
7:03 UTC
Read the original article
Hit count: 310
netbeans6.7
Hi guyz?
I have a JComboBox which i have named "venue_JCB" i want to load details from a tbl column on form load at the initcomponets() i have declared the method "accessDBinit() ;" and after the initComponets() i have implemented the accessDBinit(); method as follows: ================================code============================
void accessDBinit() //Get Student List in the List Box { try { sql = "SELECT classLevel FROM class_session ORDER BY classLevel"; java.lang.Class.forName("com.mysql.jdbc.Driver"); Connection con2=DriverManager.getConnection("jdbc:mysql:///coast","root",""); Statement stmt2 = (Statement)con2.createStatement(); boolean hasResults = stmt2.execute(sql); if(hasResults) { ResultSet rs2 = stmt2.getResultSet(); if(rs2!=null) { displayResultsinit(rs2); } db.stmt.close(); }
}
catch(Exception e)
{
System.out.println(e+"Error in connection");
}
}
void displayResultsinit(ResultSet r) throws SQLException
{
ResultSetMetaData rmeta = r.getMetaData();
int numColumns=rmeta.getColumnCount();
while(r.next())
{
for(int i=1;i<=numColumns;++i)
{
if(i<=numColumns)
{
venue_JCB.addItem(r.getString(i));
}
}
}
}
====================================end of code===================================
the method works (Populate the combobox) but at the output bar i get the following on form load and also whenever i select a component from the combobox ======= error=====
java.lang.NullPointerExceptionError in connection
could some one please tell me where i am going wrong with the method?
Thanks in advance
regards Malachi
© Stack Overflow or respective owner