Switch to full style
Graphics and animation Java code examples
Post a reply

Filling JComboBox from database

Thu Jan 31, 2013 11:23 pm

JComboBox usage example which is filling JComboBox from database, connection is passed to ComboBox.
java code
/*
* MyComboBox.java
*

*/

package connectingdatabase.Search;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JComboBox;

/**
*
* @author sami
*/
class MyComboBox extends JComboBox
{
public MyComboBox(Connection conn,String TableName)
{
if(conn==null) {
return;
}
Statement stat;
try {
stat = conn.createStatement();
ResultSet rs=stat.executeQuery("Select * from "+TableName+"; ");
ResultSetMetaData rsmd= rs.getMetaData();
int size=rsmd.getColumnCount();
for(int i=0;i<size;i++)
{
addItem(rsmd.getColumnName(i+1));
}
} catch (SQLException ex) {
ex.printStackTrace();
}

}
}




Post a reply
  Related Posts  to : Filling JComboBox from database
 filling two comboboxes     -  
 ASP.NET code for filling and displaying a DataGrid     -  
 2 different database     -  
 information about database     -  
 Selecting a Database in php     -  
 database connection     -  
 List all database in php     -  
 need help building table in my database!     -  
 array in oracle database     -  
 Close database connections in php     -  

Topic Tags

Java Swing