How to Display Data from Database in JComboBox Java Example

Display Data from Database in JComboBox

In this tutorial, We will show you simple program about, How to display data from database in JComboBox using Java. The program has been tested and output shared in the same post.

Environment Details

1. JDK 1.7
2. MySQL Database
3. Eclipse IDE

Table Query

CREATE TABLE country_details(country_id INT, country_name VARCHAR(255));

How to Display Data from Database in JComboBox Java Example

Connection Provider

package com.dineshkrish;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * 
 * @author Dinesh Krishnan
 *
 */

public class ConnectionProvider {

	private static Connection connection;

	private static final String CONNECTION_URL = "jdbc:mysql://localhost:3306/demodb";
	private static final String USERNAME = "root";
	private static final String PASSWORD = "root";
	private static final String DRIVERNAME = "com.mysql.jdbc.Driver";

	private ConnectionProvider() {

	}

	public static Connection getConnection() {

		if (connection == null) {

			try {
				Class.forName(DRIVERNAME);
				connection = DriverManager.getConnection(CONNECTION_URL, USERNAME, PASSWORD);
			} catch (SQLException e) {
				System.out.println(e.getMessage());
			} catch (ClassNotFoundException e) {
				System.out.println(e.getMessage());
			}
		}

		return connection;
	}
}

Retrieving data from Database

package com.dineshkrish;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @author Dinesh Krishnan
 *
 */

public class DataStoreImpl {

	public List<String> getCountry() {
		Connection connection = ConnectionProvider.getConnection();
		List<String> countryList = new ArrayList<String>();
		try {
			Statement stmt = connection.createStatement();
			ResultSet rs = stmt.executeQuery("SELECT country_name FROM country_details");
			while (rs.next()) {
				countryList.add(rs.getString("country_name"));
			}
		} catch (SQLException e) {
			System.out.println(e.getMessage());
		}
		return countryList;
	}
}

Displaying Data in JComboBox

package com.dineshkrish;

import java.awt.BorderLayout;
import java.util.List;

import javax.swing.JComboBox;
import javax.swing.JFrame;

/**
 * 
 * @author Dinesh Krishnan
 *
 */

public class DropDownDemo extends JFrame {

	public DropDownDemo() {

		super.setTitle("Drop Down Demo");
		super.setSize(500, 500);
		super.setVisible(true);
		createDropDown();
	}

	private void createDropDown() {

		JComboBox<String> comboBox = new JComboBox<String>();

		DataStoreImpl data = new DataStoreImpl();
		List<String> countryList = data.getCountry();

		for (String country : countryList) {
			comboBox.addItem(country);
		}

		add(comboBox, BorderLayout.NORTH);
	}

	public static void main(String[] args) {

		DropDownDemo demo = new DropDownDemo();

	}

}

Output

How to Display Data from Database in JComboBox Java Example

Hello, folks, I am a founder of dineshkrish.com. I love Java and Open source technologies, If you find my tutorials are useful, please consider making donations to these charities.