hi;
i am new to java.i want to get data from db for jcombobox.plz help me

Recommended Answers

All 2 Replies

Whether it is for combobox or something else, it is irrelevant. What you need is a way to read the info from the database and put them into a list (Vector).
Then you can use the data into the Vector and put wherever you want (JComboBox)

Search this forum, there are examples on how to connect to database using java.
You will need to create the connection, then use the query and the connection to create a statement and then run it to get the results.
Search this forum for examples

use jdbc to get the data ,here is an example

package com.bxshop.test;

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

public class Demo1 {
	public static void main(String[] args) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		try {
			//your jdbc driver name, it differs from various dbs
			Class.forName("com.mysql.jdbc.Driver");
			//and this is the url
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
			//exxcute query
			ps = conn.prepareStatement("SELECT username, password FROM test");
			rs = ps.executeQuery();
			
			while(rs.next()) {
				System.out.println(rs.getString(1) + "\t" +rs.getString(2));
			}
			rs.close();
			ps.close();
			conn.close();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.