guavamay
asked on
Passing result set from first fram to second frame
Hello,
I have a query in my first frame. What I am trying to achieve is display the list of values from my result set from my first frame to a JList in my second frame.
Here is what I've done so far....
1. building a string query from my result set in my first frame
2. by clicking my search button in my first frame, it displays the second frame.
The help I need is how to pass the result set in my second frame.
Would it be possible for somebody to give me some code example to start playing with it.
I am using netbeans and java swing.
Thanks in advance.
I have a query in my first frame. What I am trying to achieve is display the list of values from my result set from my first frame to a JList in my second frame.
Here is what I've done so far....
1. building a string query from my result set in my first frame
2. by clicking my search button in my first frame, it displays the second frame.
The help I need is how to pass the result set in my second frame.
Would it be possible for somebody to give me some code example to start playing with it.
I am using netbeans and java swing.
Thanks in advance.
How many columns are there?
ASKER
There are two columns, firstname and lastname.
Thanks so much for quick reply.
Thanks so much for quick reply.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
You mean using a JTable in second frame instead of JList?
Yes
ASKER
Oh okay. I changed it and now my second frame has JTable. Now what! How do I pass the result set from my first frame to this JTable in second frame ?
Sorry I am very new to Java.
Thanks.
Sorry I am very new to Java.
Thanks.
You can pass a TableModel. See
http://technojeeves.com/joomla/index.php/free/59-resultset-to-tablemodel
http://technojeeves.com/joomla/index.php/free/59-resultset-to-tablemodel
ASKER
Thanks. I read it and trying to understand...
1. Am i suppose to create this DBUtils class inside my class in my first frame program?
2. Ami I building the JTable in my first frame program? In that case how am I reading this JTable to my second frame program ?
Sorry to ask these basic questions as I am new to java.
Thanks again.
1. Am i suppose to create this DBUtils class inside my class in my first frame program?
2. Ami I building the JTable in my first frame program? In that case how am I reading this JTable to my second frame program ?
Sorry to ask these basic questions as I am new to java.
Thanks again.
>>1. Am i suppose to create this DBUtils class inside my class in my first frame program?
No - you don't create a class. The method is static - there's an example on the page
>>2. Ami I building the JTable in my first frame program?
No. As i said above, you pass a TableModel from the first frame code
No - you don't create a class. The method is static - there's an example on the page
>>2. Ami I building the JTable in my first frame program?
No. As i said above, you pass a TableModel from the first frame code
ASKER
Oh okay.
As for my #2 question, I am still not clear(Sorry!!).
Would it be possible for you to elaborate that using the sample code from code link you send it to me?
How do I pass that TableModel into the second frame?
Do I make a java utility call in my second frame?
Thanks.
As for my #2 question, I am still not clear(Sorry!!).
Would it be possible for you to elaborate that using the sample code from code link you send it to me?
How do I pass that TableModel into the second frame?
Do I make a java utility call in my second frame?
Thanks.
Just put a method in the second class:
public void setTableModel(TableModel model) {
table.setModel(model);
}
don't overcomplicate things :) You just need a simple loop to read the result set into a list and use that (instead of the ResultSet). No need to pass the ResultSet around. Would only be a few lines of code, no need to go importing unnecessarily complicated code.
Also be aware that you should be using something like SwingWorker to deal with event dispatch thread
Also be aware that you should be using something like SwingWorker to deal with event dispatch thread
ASKER
Hi objects,
I looked at the link you gave me. Would it be possible for you to send me some example? I do have a result set in my 'Search button ActionPerformed'. Since I am beginner, if this can be done easy way I am all for it.
What I am trying to do is... input name in Jtext field, click search button from main menu, which will display the matching records either Jlist or JTable(which CEHJ recommended) in a second frame (or something like pop up manner).
Thanks so much.
Here is my 'Search button ActionPerformed' looks like.
========================== ========== ==
private void jSearchbtnActionPerformed( java.awt.e vent.Actio nEvent evt) {
//get the input value
String value1=fname.getText();
//connect db
Connection con = null;
ResultSet searchrs = null;
ResultSetMetaData md;
String colname = null;
String temp = null;
try {
Class.forName("com.mysql.j dbc.Driver ");
con = DriverManager.getConnectio n("jdbc:my sql://loca lhost:3306 /testdb"," root","roo t");
try {
String sql =
"Select firstname,lastname, id, userid from user where firstname = '" + value1 + "' ";
PreparedStatement search = con.prepareStatement(sql);
searchrs = search.executeQuery();
md = searchrs.getMetaData();
int count = md.getColumnCount();
for (int i = 1; i<=count; i++) {
colname = md.getColumnName(i);
System.out.println (colname);
}
System.out.println(count);
//going through the result set loop
while (searchrs.next()){
temp = temp + searchrs.getString("firstN ame") + searchrs.getString("lastNa me");
}
System.out.println(temp);
search.close();
con.close();
// Calling the SearchUser.java program
// new SearchUser().setVisible(tr ue);
// build the list in 2nd frame
} catch (SQLException s){
JOptionPane.showMessageDia log(this, "SQL query did not run",
"ERROR",JOptionPane.ERROR_ MESSAGE);
s.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
I looked at the link you gave me. Would it be possible for you to send me some example? I do have a result set in my 'Search button ActionPerformed'. Since I am beginner, if this can be done easy way I am all for it.
What I am trying to do is... input name in Jtext field, click search button from main menu, which will display the matching records either Jlist or JTable(which CEHJ recommended) in a second frame (or something like pop up manner).
Thanks so much.
Here is my 'Search button ActionPerformed' looks like.
==========================
private void jSearchbtnActionPerformed(
//get the input value
String value1=fname.getText();
//connect db
Connection con = null;
ResultSet searchrs = null;
ResultSetMetaData md;
String colname = null;
String temp = null;
try {
Class.forName("com.mysql.j
con = DriverManager.getConnectio
try {
String sql =
"Select firstname,lastname, id, userid from user where firstname = '" + value1 + "' ";
PreparedStatement search = con.prepareStatement(sql);
searchrs = search.executeQuery();
md = searchrs.getMetaData();
int count = md.getColumnCount();
for (int i = 1; i<=count; i++) {
colname = md.getColumnName(i);
System.out.println (colname);
}
System.out.println(count);
//going through the result set loop
while (searchrs.next()){
temp = temp + searchrs.getString("firstN
}
System.out.println(temp);
search.close();
con.close();
// Calling the SearchUser.java program
// new SearchUser().setVisible(tr
// build the list in 2nd frame
} catch (SQLException s){
JOptionPane.showMessageDia
"ERROR",JOptionPane.ERROR_
s.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
just read the data into a List, thats all you need:
private void jSearchbtnActionPerformed( java.awt.e vent.Actio nEvent evt) {
//get the input value
String value1=fname.getText();
//connect db
Connection con = null;
ResultSet searchrs = null;
try {
Class.forName("com.mysql.j dbc.Driver ");
con = DriverManager.getConnectio n("jdbc:my sql://loca lhost:3306 /testdb"," root","roo t");
try {
String sql =
"Select firstname,lastname, id, userid from user where firstname = '" + value1 + "' ";
PreparedStatement search = con.prepareStatement(sql);
searchrs = search.executeQuery();
List list = new ArrayList();
//going through the result set loop
while (searchrs.next()){
list.add(searchrs.getStrin g("firstNa me") + searchrs.getString("lastNa me"));
}
System.out.println(list);
search.close();
con.close();
// Calling the SearchUser.java program
// new SearchUser().setVisible(tr ue);
// build the list in 2nd frame
} catch (SQLException s){
JOptionPane.showMessageDia log(this, "SQL query did not run",
"ERROR",JOptionPane.ERROR_ MESSAGE);
s.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void jSearchbtnActionPerformed(
//get the input value
String value1=fname.getText();
//connect db
Connection con = null;
ResultSet searchrs = null;
try {
Class.forName("com.mysql.j
con = DriverManager.getConnectio
try {
String sql =
"Select firstname,lastname, id, userid from user where firstname = '" + value1 + "' ";
PreparedStatement search = con.prepareStatement(sql);
searchrs = search.executeQuery();
List list = new ArrayList();
//going through the result set loop
while (searchrs.next()){
list.add(searchrs.getStrin
}
System.out.println(list);
search.close();
con.close();
// Calling the SearchUser.java program
// new SearchUser().setVisible(tr
// build the list in 2nd frame
} catch (SQLException s){
JOptionPane.showMessageDia
"ERROR",JOptionPane.ERROR_
s.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
you can then use that list to populate your gui
ASKER
Thank you very much.
I can see the lArray list is building with my records now. I found some example, tried to use it to populate the list in my JTable. I must be not doing it right as I don't see my Jtable popping up. What am I doing wrong? Is this the way to do it or did I place it wrong?
Any help would be greatly appreciated.
Thanks again.
Here is my updated code........
private void jSearchbtnActionPerformed(
//get the input value
String value1=fname.getText();
//connect db
Connection con = null;
ResultSet searchrs = null;
try {
Class.forName("com.mysql.j
con = DriverManager.getConnectio
try {
String sql =
"Select firstname,lastname, id, userid from user where firstname = '" + value1 + "' ";
PreparedStatement search = con.prepareStatement(sql);
searchrs = search.executeQuery();
List<String> list = new ArrayList<String>();
//Read the result set in the following loop and build arraylist
//which then will be used to build in jtable
while (searchrs.next()){
list.add(searchrs.getStrin
}
System.out.println(list);
search.close();
con.close();
// Build the list in the Jtable
Object tbl1[] = list.toArray();
JTable tbldisplay = new JTable();
DefaultTableModel model = (DefaultTableModel)tbldisp
for (int d=0; d< list.size(); d++) {
model.addRow(new Object[]{tbl1[d]});
}
tbldisplay.setModel(model)
tbldisplay.setVisible(true
} catch (SQLException s){
JOptionPane.showMessageDia
"ERROR",JOptionPane.ERROR_
s.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
doesn't look like you should be using a JTable, a JList would seem more appropriate
ASKER
Thanks so much for quick reply.
Do I need to put JList in my main frame? Do you have any example?
Thanks again.
Do I need to put JList in my main frame? Do you have any example?
Thanks again.
you do, see the example here
http://helpdesk.objects.com.au/java/how-do-i-add-scroll-bars-to-my-jlist
http://helpdesk.objects.com.au/java/how-do-i-add-scroll-bars-to-my-jlist
>>Is this the way to do it or did I place it wrong?
That code is nearly there. The main thing is that your objective is to set the TableModel on a table in another window, so you need to make a method in that window that sets the model on its table. I called that 'setTableModel' below, and 'otherFrame' is your other window.
It can be simplified too the following. Make sure the DBUtils jar is in your classpath
That code is nearly there. The main thing is that your objective is to set the TableModel on a table in another window, so you need to make a method in that window that sets the model on its table. I called that 'setTableModel' below, and 'otherFrame' is your other window.
It can be simplified too the following. Make sure the DBUtils jar is in your classpath
private void jSearchbtnActionPerformed(java.awt.event.ActionEvent evt) {
//get the input value
String value1 = fname.getText();
//connect db
Connection con = null;
ResultSet searchrs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb",
"root", "root");
try {
// Select only the needed columns
String sql = "Select firstname,lastname from user where firstname = ?";
PreparedStatement search = con.prepareStatement(sql);
search.setString(1, value1);
searchrs = search.executeQuery();
otherFrame.setTableModel(DBUtils.resultSetToTableModel(searchrs));
search.close();
con.close();
} catch (SQLException s) {
JOptionPane.showMessageDialog(this, "SQL query did not run",
"ERROR", JOptionPane.ERROR_MESSAGE);
s.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
ASKER
Sorry, I am a bit confused here....
I am not sure exactly where I need to place the setTableModel line!
Am I suppose to leave the code as is with the array list (the updated one I just posted ) and just add the " otherFrame.setTableModel(D BUtils.res ultSetToTa bleModel(s earchrs)); " before I close the connection?
Thank You.
I am not sure exactly where I need to place the setTableModel line!
Am I suppose to leave the code as is with the array list (the updated one I just posted ) and just add the " otherFrame.setTableModel(D
Thank You.
> I am not sure exactly where I need to place the setTableModel line!
you don't need a TableModel at all :)
you can simply create a JList with your list
JList jlist = new JList(list);
then add jlist to your gui
private void jSearchbtnActionPerformed( java.awt.e vent.Actio nEvent evt) {
//get the input value
String value1=fname.getText();
//connect db
Connection con = null;
ResultSet searchrs = null;
try {
Class.forName("com.mysql.j dbc.Driver ");
con = DriverManager.getConnectio n("jdbc:my sql://loca lhost:3306 /testdb"," root","roo t");
try {
String sql =
"Select firstname,lastname, id, userid from user where firstname = '" + value1 + "' ";
PreparedStatement search = con.prepareStatement(sql);
searchrs = search.executeQuery();
List list = new ArrayList();
//Read the result set in the following loop and build arraylist
//which then will be used to build in jtable
while (searchrs.next()){
list.add(searchrs.getStrin g("firstna me") + searchrs.getString("lastna me"));
}
System.out.println(list);
search.close();
con.close();
JList jlist = new JList(list);
JFrame frame = new JFrame();
frame.getContentPane().add (new JScrollPane(jlist));
frame.pack();
frame.show();
}
All done :)
you don't need a TableModel at all :)
you can simply create a JList with your list
JList jlist = new JList(list);
then add jlist to your gui
private void jSearchbtnActionPerformed(
//get the input value
String value1=fname.getText();
//connect db
Connection con = null;
ResultSet searchrs = null;
try {
Class.forName("com.mysql.j
con = DriverManager.getConnectio
try {
String sql =
"Select firstname,lastname, id, userid from user where firstname = '" + value1 + "' ";
PreparedStatement search = con.prepareStatement(sql);
searchrs = search.executeQuery();
List list = new ArrayList();
//Read the result set in the following loop and build arraylist
//which then will be used to build in jtable
while (searchrs.next()){
list.add(searchrs.getStrin
}
System.out.println(list);
search.close();
con.close();
JList jlist = new JList(list);
JFrame frame = new JFrame();
frame.getContentPane().add
frame.pack();
frame.show();
}
All done :)
don't overcomplicate what is actually a simple problem
>>
Sorry, I am a bit confused here....
I am not sure exactly where I need to place the setTableModel line!
>>
setTableModel should be a method in the second frame, so you need to make one as i mentioned above. You don't need to alter anything in the code i posted.
The code using JList won't help you:
a. The component isn't appropriate for tabular data
b. The component is not in the class where you're fetching the data
Sorry, I am a bit confused here....
I am not sure exactly where I need to place the setTableModel line!
>>
setTableModel should be a method in the second frame, so you need to make one as i mentioned above. You don't need to alter anything in the code i posted.
The code using JList won't help you:
a. The component isn't appropriate for tabular data
b. The component is not in the class where you're fetching the data
> a. The component isn't appropriate for tabular data
Actually a JList is perfect for data involved
> b. The component is not in the class where you're fetching the data
thats completely unfounded. And irrelevant, it can be move to whereever it is needed.
Don't make claims without any supporting evidence it just confuses people.
Try the JList code and let me know how ti goes and if any tweaking is required
guavamay, let me know if you have any further questions
ASKER
Hi objects,
I did exactly the way you showed here. But I am getting an error message on this line .. JList jlist = new JList(list);
I added the jlist in my frame.
The error msg is ...can not find symbol : constructor JList(java.util.ArrayList< java.lang. String>)
location: class javax.swing.JList
JList jlist = new JList(list);
1 error
Here is the code......
System.out.println(list);
//Close all the connection
search.close();
con.close();
//Build Jlist
JList jlist = new JList(list);
JFrame frame = new JFrame();
frame.getContentPane().add (new JScrollPane(jlist));
frame.pack();
frame.setVisible(true);
Thanks.
I did exactly the way you showed here. But I am getting an error message on this line .. JList jlist = new JList(list);
I added the jlist in my frame.
The error msg is ...can not find symbol : constructor JList(java.util.ArrayList<
location: class javax.swing.JList
JList jlist = new JList(list);
1 error
Here is the code......
System.out.println(list);
//Close all the connection
search.close();
con.close();
//Build Jlist
JList jlist = new JList(list);
JFrame frame = new JFrame();
frame.getContentPane().add
frame.pack();
frame.setVisible(true);
Thanks.
> JList jlist = new JList(list);
sorry my bad, should be:
JList jlist = new JList(list.toArray());
sorry my bad, should be:
JList jlist = new JList(list.toArray());
>> What I am trying to achieve is display the list of values from my result set from my first frame to a JList in my second frame.
Have you now decided not to do this guavamay?
Have you now decided not to do this guavamay?
ASKER
Hi CEHJ,
I am getting error msg saying 'cannot find symbol, variable DButils' on the following line.
otherFrame.setTableModel(D BUtils.res ultSetToTa bleModel(s earchrs));
I sort of understand the error message but dont know yet how to fix it.
How do I fix that?
Thanks.
I am getting error msg saying 'cannot find symbol, variable DButils' on the following line.
otherFrame.setTableModel(D
I sort of understand the error message but dont know yet how to fix it.
How do I fix that?
Thanks.
> I am getting error msg saying 'cannot find symbol, variable DButils' on the following line.
you don't need to call dbutils. All you is loop you have that populates the list
you don't need to call dbutils. All you is loop you have that populates the list
>>
I sort of understand the error message but dont know yet how to fix it.
How do I fix that?
>>
See http:#26415826
>>Make sure the DBUtils jar is in your classpath
I sort of understand the error message but dont know yet how to fix it.
How do I fix that?
>>
See http:#26415826
>>Make sure the DBUtils jar is in your classpath
ASKER
Sorry CEHJ, I am very new to Java. I tried to find through internet but still can't find how to check if DBUtils jar is in my classpath. How do I set that up.
I am using NetBeans. I am trying to implement your method of Jtable but stuck on that DBUtils error. Please help!
Thanks so much.
I am using NetBeans. I am trying to implement your method of Jtable but stuck on that DBUtils error. Please help!
Thanks so much.
you don't need dbutils (or any other external libraries). did you try the code I posted above, it should be all you need.
private void jSearchbtnActionPerformed( java.awt.e vent.Actio nEvent evt) {
//get the input value
String value1=fname.getText();
//connect db
Connection con = null;
ResultSet searchrs = null;
try {
Class.forName("com.mysql.j dbc.Driver ");
con = DriverManager.getConnectio n("jdbc:my sql://loca lhost:3306 /testdb"," root","roo t");
try {
String sql =
"Select firstname,lastname, id, userid from user where firstname = '" + value1 + "' ";
PreparedStatement search = con.prepareStatement(sql);
searchrs = search.executeQuery();
List list = new ArrayList();
//Read the result set in the following loop and build arraylist
//which then will be used to build in jtable
while (searchrs.next()){
list.add(searchrs.getStrin g("firstna me") + searchrs.getString("lastna me"));
}
System.out.println(list);
search.close();
con.close();
JList jlist = new JList(list.toArray());
JFrame frame = new JFrame();
frame.getContentPane().add (new JScrollPane(jlist));
frame.pack();
frame.show();
}
private void jSearchbtnActionPerformed(
//get the input value
String value1=fname.getText();
//connect db
Connection con = null;
ResultSet searchrs = null;
try {
Class.forName("com.mysql.j
con = DriverManager.getConnectio
try {
String sql =
"Select firstname,lastname, id, userid from user where firstname = '" + value1 + "' ";
PreparedStatement search = con.prepareStatement(sql);
searchrs = search.executeQuery();
List list = new ArrayList();
//Read the result set in the following loop and build arraylist
//which then will be used to build in jtable
while (searchrs.next()){
list.add(searchrs.getStrin
}
System.out.println(list);
search.close();
con.close();
JList jlist = new JList(list.toArray());
JFrame frame = new JFrame();
frame.getContentPane().add
frame.pack();
frame.show();
}
>>I am using NetBeans. I am trying to implement your method of Jtable but stuck on that DBUtils error. Please help!
Just add DBUtils.jar to your project - it's as simple as that
Just add DBUtils.jar to your project - it's as simple as that
ASKER
I dont think I have DBUtils.jar file, I searched for it in my computer..
Is this jar file I have to download from sun website? (Sorry!!)
I do know now how to add that jar file in my project but where can I find this jar file Please?
Thanks.
Is this jar file I have to download from sun website? (Sorry!!)
I do know now how to add that jar file in my project but where can I find this jar file Please?
Thanks.
one more time :) you do not need DBUtils.jar
have you tried the code I posted above, it should run fine for you.
have you tried the code I posted above, it should run fine for you.
ASKER
Hi objects,
I certainly did and thank you so very much for your help. It is working and as a beginner java programmer, it is a lot easier solution. But I have to use JTable as I can re use it later on to edit or add the records in the JTable in my project.
Thanks again.
I certainly did and thank you so very much for your help. It is working and as a beginner java programmer, it is a lot easier solution. But I have to use JTable as I can re use it later on to edit or add the records in the JTable in my project.
Thanks again.
ASKER
Do you know where can I find the DBUtils.jar file?
Thanks.
Thanks.
thats fine but in your question you asked how to put it in a JList. Which I have answered. So you should accept my comment, and open a new question to avoid ongoing confusion and ask how to use a JTable instead of a JList (its actually pretty trivial).
> Do you know where can I find the DBUtils.jar file?
you don't need it (or any 3rd party code). just modify the loop above to populate your table model instead of a list model. Thats the only change that would be required.
> Do you know where can I find the DBUtils.jar file?
you don't need it (or any 3rd party code). just modify the loop above to populate your table model instead of a list model. Thats the only change that would be required.
actual the changes for a JTable are really trivial.
you should just need something like this
private void jSearchbtnActionPerformed( java.awt.e vent.Actio nEvent evt) {
//get the input value
String value1=fname.getText();
//connect db
Connection con = null;
ResultSet searchrs = null;
try {
Class.forName("com.mysql.j dbc.Driver ");
con = DriverManager.getConnectio n("jdbc:my sql://loca lhost:3306 /testdb"," root","roo t");
try {
String sql =
"Select firstname,lastname, id, userid from user where firstname = '" + value1 + "' ";
PreparedStatement search = con.prepareStatement(sql);
searchrs = search.executeQuery();
DefaultTableModel model = new DefaultTableModel(new Object[] { "First", "Last"});
//Read the result set in the following loop and build arraylist
//which then will be used to build in jtable
while (searchrs.next()){
model.addRow(new Object[] {searchrs.getString("first name"), searchrs.getString("lastna me")});
}
search.close();
con.close();
JTable table = new JTable(model);
JFrame frame = new JFrame();
frame.getContentPane().add (new JScrollPane(table));
frame.pack();
frame.show();
}
you should just need something like this
private void jSearchbtnActionPerformed(
//get the input value
String value1=fname.getText();
//connect db
Connection con = null;
ResultSet searchrs = null;
try {
Class.forName("com.mysql.j
con = DriverManager.getConnectio
try {
String sql =
"Select firstname,lastname, id, userid from user where firstname = '" + value1 + "' ";
PreparedStatement search = con.prepareStatement(sql);
searchrs = search.executeQuery();
DefaultTableModel model = new DefaultTableModel(new Object[] { "First", "Last"});
//Read the result set in the following loop and build arraylist
//which then will be used to build in jtable
while (searchrs.next()){
model.addRow(new Object[] {searchrs.getString("first
}
search.close();
con.close();
JTable table = new JTable(model);
JFrame frame = new JFrame();
frame.getContentPane().add
frame.pack();
frame.show();
}
ASKER
Thank you sooo much, I really appreciate all your help!
I am still getting an error on the following line.
DefaultTableModel model = new DefaultTableModel(new Object[] { "First", "Last"});
The error is : can not find symbol, symbol: Constructor DefaultTableModel(java.lan g.object[] ).
Thanks again.
I am still getting an error on the following line.
DefaultTableModel model = new DefaultTableModel(new Object[] { "First", "Last"});
The error is : can not find symbol, symbol: Constructor DefaultTableModel(java.lan
Thanks again.
just need to add the import
import javax.swing.table.*;
import javax.swing.table.*;
ASKER
I already added that!
Here is my code now looks like:
import javax.swing.table.*; // this one I have at the top of my program
private void jSearchbtnActionPerformed( java.awt.e vent.Actio nEvent evt) {
//get the input value
String value1=fname.getText();
//connect db
Connection con = null;
ResultSet searchrs = null;
try {
Class.forName("com.mysql.j dbc.Driver ");
con = DriverManager.getConnectio n("jdbc:my sql://loca lhost:3306 /testdb"," root","roo t");
try {
String sql =
"Select firstname, lastname from user where firstname = '" + value1 + "' ";
PreparedStatement search = con.prepareStatement(sql);
searchrs = search.executeQuery();
System.out.println(searchr s);
DefaultTableModel model = new DefaultTableModel(new Object[]{"firstname","last name"});
//Read the result set in the following loop and build
while (searchrs.next()){
//model.addColumn(fname);
model.addRow(new Object[] {searchrs.getString("first name"), searchrs.getString("lastna me")});
}
search.close();
con.close();
//Build JTable
JTable table = new JTable(model);
JFrame frame = new JFrame();
frame.getContentPane().add (new JScrollPane(table));
frame.pack();
frame.setVisible(true);
} catch (SQLException s){
JOptionPane.showMessageDia log(this, "SQL query did not run",
"ERROR",JOptionPane.ERROR_ MESSAGE);
s.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
What am I doing wrong?
Thanks.
Here is my code now looks like:
import javax.swing.table.*; // this one I have at the top of my program
private void jSearchbtnActionPerformed(
//get the input value
String value1=fname.getText();
//connect db
Connection con = null;
ResultSet searchrs = null;
try {
Class.forName("com.mysql.j
con = DriverManager.getConnectio
try {
String sql =
"Select firstname, lastname from user where firstname = '" + value1 + "' ";
PreparedStatement search = con.prepareStatement(sql);
searchrs = search.executeQuery();
System.out.println(searchr
DefaultTableModel model = new DefaultTableModel(new Object[]{"firstname","last
//Read the result set in the following loop and build
while (searchrs.next()){
//model.addColumn(fname);
model.addRow(new Object[] {searchrs.getString("first
}
search.close();
con.close();
//Build JTable
JTable table = new JTable(model);
JFrame frame = new JFrame();
frame.getContentPane().add
frame.pack();
frame.setVisible(true);
} catch (SQLException s){
JOptionPane.showMessageDia
"ERROR",JOptionPane.ERROR_
s.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
What am I doing wrong?
Thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ahhh!
Thank you ever so much. Its working.
[Its perfect for now. Later on I will add buttons to that table or something like to be able to modify. Anyhow, that probably would be another question!]
Originally I know I have JList in my question, but because of CEHJ's suggestion I learn the JTable concept. So I thought I gave him 50 points for that (I hope you don't mind!!) The rest is yours.
I like to thank you both for your time, specially you objects. Because of you both, I already learned a lot as a beginner Java Programmer!
Thanks again
Thank you ever so much. Its working.
[Its perfect for now. Later on I will add buttons to that table or something like to be able to modify. Anyhow, that probably would be another question!]
Originally I know I have JList in my question, but because of CEHJ's suggestion I learn the JTable concept. So I thought I gave him 50 points for that (I hope you don't mind!!) The rest is yours.
I like to thank you both for your time, specially you objects. Because of you both, I already learned a lot as a beginner Java Programmer!
Thanks again
> but because of CEHJ's suggestion I learn the JTable concept.
just because your result set has two columns doesn't mean you should use a JTable :)
just because your result set has two columns doesn't mean you should use a JTable :)
ASKER
Hi there,
Actually I just started with two columns. Because I am just learning all these. I will now add all the rest of the columns.
Thanks again.
Actually I just started with two columns. Because I am just learning all these. I will now add all the rest of the columns.
Thanks again.
>>
Do you know where can I find the DBUtils.jar file?
Thanks.
>>
It's here:
http://technojeeves.com/tech/rs2xml.jar
Avoid reinventing the wheel. Always use code that contains functionality you need rather than writing it yourself
Do you know where can I find the DBUtils.jar file?
Thanks.
>>
It's here:
http://technojeeves.com/tech/rs2xml.jar
Avoid reinventing the wheel. Always use code that contains functionality you need rather than writing it yourself