JComboBox with database making problem on retrive
HI All!:frusty:
In my project i am using JComboBox with Database
I wants to retrives the date from database to JComboBox
my code is
Code:
public void showcom()
{
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn=(Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/payslip","root","root");
PreparedStatement ps=conn.prepareStatement("select EmployeeID from newemp");
//PreparedStatement ps=conn.prepareStatement("select * from newemp");
ResultSet rs=ps.executeQuery();
if(rs.next())
{
combob.addItem(rs.getString("EmployeeID"));
}
}
catch(Exception e)
{
}
}
I have near with 50 record in that table.
but it showing the first employee id only, doesn't show the remaining Employee ID's
what is the problem i did in my cod,
can anyone Guide me..,
Thank you..,
Re: JComboBox with database making problem on retrive
You're only adding one record to your combo box, no matter how many records are stored in your table; add a loop somewhere in your code, not just an if-statement.
kind regards,
Jos
Re: JComboBox with database making problem on retrive
Thank You Mr.Jos!
i will try now self..,
Thanks For your Idea sir..,
Re: JComboBox with database making problem on retrive
Yes, that 'if' should be a 'while'.
Re: JComboBox with database making problem on retrive
sir!
because of my mistaken i receive the same problem,
the first records only repeated as numbers of rows..,
how to i correct in that loop sir..,
Code:
if(rs.next())
{
for(int i=0;i<=rs.getRow();i++)
{
jComboBox1.addItem(rs.getString("EmployeeID"));
}
}
Re: JComboBox with database making problem on retrive
You're using the wrong method (read the API documentation for the ResultSet interface); you should use a combination of rs.next(), rs.getXXX() method calls.
kind regards,
Jos
Re: JComboBox with database making problem on retrive
sir i try with google help,
at the time i was found the fallowing samples sir..,
Add Items in JComboBox from Access Database
http://www.java-forums.org/jdbc/1510...base-data.html
in that sample code are using the same methods that's why i use..,
ok sir! please guide me, what i have to do now..,
Re: JComboBox with database making problem on retrive
RoseIndia is an extremey crappy site; use a simple loop:
Code:
while (rs.next()) {
// fetch a column and add it to your combo box
}
kind regards,
Jos
Re: JComboBox with database making problem on retrive
Which is what I said...it's amazing how sometimes our posts simply go invisible.
Re: JComboBox with database making problem on retrive
Quote:
Originally Posted by
Tolls
Which is what I said...it's amazing how sometimes our posts simply go invisible.
Duh, but I said it first (with an excellent eloquent verbiage, if I may say so myself ;-)
kindest regards,
Jos
Re: JComboBox with database making problem on retrive
OT (partly): I wish Google would catch on to whatever nefarious scheming keeps that worthless stinking waste of bandwidth at or near the top of their searches.
The first time I discovered the site, I found a code 'example' that extended JFrame but wasn't used as one; the JFrame that was shown was declared and constructed inside a method; a local variable. Months later, they hadn't corrected the code. Chances are nobody even read my feedback.
db
Re: JComboBox with database making problem on retrive
Are you criticizing that beautiful gem? That source of wisdom? The holy knowledge base that enlightens millions of poor souls, searching for the ultimate truth? tsk, tsk ...
kind regards,
Jos ;-)
Re: JComboBox with database making problem on retrive
Their servers should be immersed in the Ganges. Along with their coders, who by no stretch of the imagination can be called programmers.
db
Re: JComboBox with database making problem on retrive
sorry for the late response sir..,
i am living in out of city that's why i unable to get good internet connection,
that is the only reason for my invisibility..,
forgive me please for my invisibility with this Forum, while you response for my post..,
Re: JComboBox with database making problem on retrive
Thanks For My Guides:(y):
Jest i try my self, it is working well
instead of using "if" i used "while" loop and remove the "for" condition
my code is
Code:
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn=(Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/payslip","root","root");
PreparedStatement ps=conn.prepareStatement("select EmployeeID from newemp");
ResultSet rs=ps.executeQuery();
while(rs.next())
{
{
jComboBox1.addItem(rs.getString("EmployeeID"));
}
}
}
catch(Exception e)
{
JOptionPane.showMessageDialog(rootPane, e);
}
it add all the elements from my MySQL to Java Coding..,
Thank you For All..,
Re: JComboBox with database making problem on retrive
It's not your invisibility.
It's that you didn't seem to acknowledge the answers given by us here.
If you didn't understand them then just ask, but not taking in what we say leaves us a little lost as to what problems you are having.
Re: JComboBox with database making problem on retrive
ok sir!
i can understand ..,