12 Replies - 2682 Views - Last Post: 04 May 2013 - 06:40 AM Rate Topic: ***-- 2 Votes

#1 balraj8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 29-October 12

how to get database table column values to jcombobox in java

Posted 03 May 2013 - 05:31 AM

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.sql.*;
import java.util.*;


public class combo1
{
JComboBox book;
Connection con;
PreparedStatement st;
ResultSet rs;

JComboBox cb; 

public combo1()
{
JFrame f=new JFrame("New Student Details");
JPanel dtls1= new JPanel();
 try {
        	Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
	System.out.println("driver established");
	con=DriverManager.getConnection("jdbc:odbc:oradsn","system","kumar");
	System.out.println("pwd checked");
        
	Statement st = con.createStatement();
        	String c = "Select bookname from combo";
        	ResultSet res = st.executeQuery(c);
	//Vector<Object> v=new Vector<Object>();
	
	while(res.next())
	{
	cb.addItem(res.getString("bookname"));
	


	//String ids=res.getString("bookname");
	//DefaultComboBoxModel model=new DefaultComboBoxModel(ids);
	//cb.setModel(model);
	//v.add(ids);
	//v.add(res.getString("bookname"));
	cb.addItem(res.getString("bookname"));
	//cb.setModel(new DefaultComboBoxModel(ids));
	
	}
	
}
catch (Exception d) {  //System.out.println(d);  
}

dtls1.add(cb);
f.add(dtls1);
f.setSize(500,500);
f.setVisible(true);

}

public static void main(String[] ars)throws Exception
{
new combo();
}
} 



Is This A Good Question/Topic? 0
  • +

Replies To: how to get database table column values to jcombobox in java

#2 Gungnir  Icon User is offline

  • Your Imaginary Friend

Reputation: 152
  • View blog
  • Posts: 527
  • Joined: 21-May 11

Re: how to get database table column values to jcombobox in java

Posted 03 May 2013 - 05:56 AM

Do you have a question?
Was This Post Helpful? -2
  • +
  • -

#3 balraj8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 29-October 12

Re: how to get database table column values to jcombobox in java

Posted 03 May 2013 - 06:03 AM

This code is executing successfully but the problem is, its showing only the last column string value from the database, and on the command prompt it is showing all the string values of that particular column(bookname), so i need all the column values from database as combobox items.

Thanks in advance, if any1 could help me
Was This Post Helpful? -1
  • +
  • -

#4 Gungnir  Icon User is offline

  • Your Imaginary Friend

Reputation: 152
  • View blog
  • Posts: 527
  • Joined: 21-May 11

Re: how to get database table column values to jcombobox in java

Posted 03 May 2013 - 06:43 AM

I'm sorry for inconveniencing you by requesting a little bit of context. Why couldn't you have put that in your post? I hardly think that merits a Posted Image vote. Your code is poorly tabbed, your variable names are unconventional, and by extension, your code is hard to read -- on top of that, it was accompanied with no text. We can't just compile the code, because we're unable to execute DriverManager.getConnection("jdbc:odbc:oradsn","system","kumar"). Next time you ask a question, know that the more information you give us, the better we can help you. Please read the </Dream.In.Code> homework policy. Please ask a question if you expect an answer -- I think that should go without saying, but apparently not for some people.

Why do you try to add the item twice to the JComboBox? You were on the right track with the vector. You also haven't called a JComboBox constructor anywhere that I can see.

Vector cbItems = new Vector();
while(res.next())
    cbItems.add(res.getString("bookname"));

final DefaultComboBoxModel cbModel = new DefaultComboBoxModel(cbItems);
JComboBox cb = new JComboBox(cbModel);



Further Reading:
- Oracle Docs > How to Use Combo Boxes [Tutorial]

This post has been edited by Gungnir: 03 May 2013 - 06:47 AM

Was This Post Helpful? 1
  • +
  • -

#5 balraj8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 29-October 12

Re: how to get database table column values to jcombobox in java

Posted 03 May 2013 - 07:15 AM

@gungnir thanks for ur repli, sorry as i didn't understand wat u want to convery in ur 1st repli..

i applied wat u said, it is retrieving all the column values from db, but displaying only one value on the jcombobox of a frame.
i'll be thankful to u if u sort this problem for me.

and as far ur question is concerned
DriverManager.getConnection("jdbc:odbc:oradsn","system","kumar") it is a jdbc odbc driver to connect to the db.

i'm new to this forum, if i made any mistakes please ignore and kindly help me
Was This Post Helpful? 1
  • +
  • -

#6 Gungnir  Icon User is offline

  • Your Imaginary Friend

Reputation: 152
  • View blog
  • Posts: 527
  • Joined: 21-May 11

Re: how to get database table column values to jcombobox in java

Posted 03 May 2013 - 07:55 AM

What are the contents of Select bookname from combo? What should be showing up in your JComboBox? Just so we know that there is, in fact, not just one entry. :P
Was This Post Helpful? 0
  • +
  • -

#7 balraj8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 29-October 12

Re: how to get database table column values to jcombobox in java

Posted 03 May 2013 - 08:07 AM

the contents of that select statement is, all the books names available in the database, and the jcombobox must show all the list of that books. in it
Was This Post Helpful? 0
  • +
  • -

#8 Gungnir  Icon User is offline

  • Your Imaginary Friend

Reputation: 152
  • View blog
  • Posts: 527
  • Joined: 21-May 11

Re: how to get database table column values to jcombobox in java

Posted 03 May 2013 - 08:44 AM

Quote

all the books names available in the database


And what are they? Or how many of them are there?
Was This Post Helpful? 0
  • +
  • -

#9 balraj8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 29-October 12

Re: how to get database table column values to jcombobox in java

Posted 03 May 2013 - 08:50 AM

They will be around 25-30 items in the column, and i need to get them in jcombobox
Was This Post Helpful? 0
  • +
  • -

#10 Gungnir  Icon User is offline

  • Your Imaginary Friend

Reputation: 152
  • View blog
  • Posts: 527
  • Joined: 21-May 11

Re: how to get database table column values to jcombobox in java

Posted 03 May 2013 - 09:28 AM

You could always check.

Vector cbItems = new Vector();
for(int i = 0; i < 100; i++)
{
    try {
        res.next();
        cbItems.add(res.getString(1));
    } catch(Exception e) {
        e.printStackTrace();
        System.out.println("There are " + i + " entries");
        break;
    }
}

final DefaultComboBoxModel cbModel = new DefaultComboBoxModel(cbItems);
JComboBox cb = new JComboBox(cbModel);


This post has been edited by Gungnir: 03 May 2013 - 09:29 AM

Was This Post Helpful? 1
  • +
  • -

#11 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2718
  • View blog
  • Posts: 11,431
  • Joined: 20-September 08

Re: how to get database table column values to jcombobox in java

Posted 03 May 2013 - 12:07 PM

Quote

new combo();

and that's not even the class in which that code appears
Was This Post Helpful? 1
  • +
  • -

#12 balraj8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 29-October 12

Re: how to get database table column values to jcombobox in java

Posted 04 May 2013 - 04:37 AM

Thanks its working, but the problem is by default its selecting the first item in the list, how to avoid that, and when a user selects an option/item that must be stored in the db
Was This Post Helpful? 0
  • +
  • -

#13 Gungnir  Icon User is offline

  • Your Imaginary Friend

Reputation: 152
  • View blog
  • Posts: 527
  • Joined: 21-May 11

Re: how to get database table column values to jcombobox in java

Posted 04 May 2013 - 06:40 AM

Quote

but the problem is by default its selecting the first item in the list, how to avoid that

In the JComboBox? Is that not expected? I linked you to a tutorial before that covered the subject.

ORACLE said:

petList.setSelectedIndex(4);



If that wasn't the subject of your issue, I apologise. Your question may require refinement.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1