Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Regarding Listener  RSS feed

 
adeeb alexander
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I am able to directly drive the data from the database in the panel.
The code is :

try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection("jdbc dbc:addissue","library","adeeb");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM addissue ");
ResultSetMetaData md = rs.getMetaData();
int col = md.getColumnCount();

for (int i=1;i<=col;i++)
{
colnames.addElement(md.getColumnName(i));
}
while(rs.next())
{
Vector row1 = new Vector(col);
for(int i=1;i<=col;i++)
{
row1.addElement(rs.getObject(i));
}
tbdata.addElement(row1);
}
rs.close();
stmt.close();
//conn.close();
}
catch(Exception ce)
{
System.out.println("Exception:"+ce);
}

Till here it works fine, whenever i start the application i can see the table
But the problem is i am trying to do the same using a button i.e i have also added a button to the panel but when i am clicking it i am not getting the out put i cant understand why, and where the problem is. Here is the code:

show.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection("jdbc dbc:addissue","library","bismillah");
String rollno = ftf.getText();
int roll = Integer.parseInt(rollno);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM addissue WHERE rollno ="+roll);
ResultSetMetaData md = rs.getMetaData();
int col = md.getColumnCount();

for (int i=1;i<=col;i++)
{
colnames.addElement(md.getColumnName(i));
}
while(rs.next())
{
Vector row1 = new Vector(col);
for(int i=1;i<=col;i++)
{
row1.addElement(rs.getObject(i));
}
tbdata.addElement(row1);
}
rs.close();
stmt.close();
//conn.close();
}
catch(Exception ce)
{
System.out.println("Exception:"+ce);
}
}
});
please some one help me out of it please!
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it throwing some exception at console ?? Maybe your listener is not registered properly !!
[ June 09, 2008: Message edited by: Sagar Rohankar ]
 
adeeb alexander
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes it is giving null pointer exception. Now what should i do
 
adeeb alexander
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes it is giving null pointer exception. Now what should i do
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On which line it raising the error ..
PL paste your exception message here .
 
satishkumar janakiraman
Ranch Hand
Posts: 334
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, post your exception trace otherwise no one knows where exactly the exception is thrown
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use the "CODE" tags in future. It makes your code snippets more readable.
 
adeeb alexander
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
here is the code, i have the whole program. It's quite lengthy i have written it in only one file. I am unable to copy the whole code here.
The main problem is that i have compiled it and dint get any errors. I got errors when i tried to execute it. Every thing is fine just i want the data from database which is found when i click on find button, but i am unable to get it. I have left the find button listener code empty after i have tried a lot. I also cannot give before line number for before null pointer exception because i have changed my program now totally now what i need is to just get the data from database in the table, all the rest is fine now.So please help me. This is the layout of panel and code for its components.

ftf = new JTextField(20);
ftf1 = new JTextField(20);
froll = new JLabel("Roll No");
fname = new JLabel("Name");
find = new JButton("Find");
tb = new JTable();

GroupLayout gl5 = new GroupLayout(jp5);
jp5.setLayout(gl5);
gl5.setAutoCreateGaps(true);
gl5.setAutoCreateContainerGaps(true);
gl5.setHorizontalGroup(gl5.createSequentialGroup()
.addGroup(gl5.createParallelGroup(LEADING)
.addComponent(froll)
.addComponent(fname))
.addGroup(gl5.createParallelGroup(LEADING)
.addComponent(ftf)
.addComponent(ftf1)
.addComponent(find, (CENTER))
.addComponent(tb, (CENTER)))
);

gl5.setVerticalGroup(gl5.createSequentialGroup()
.addGroup(gl5.createParallelGroup(BASELINE)
.addComponent(froll)
.addComponent(ftf))
.addGroup(gl5.createParallelGroup(BASELINE)
.addComponent(fname)
.addComponent(ftf1))
.addGroup(gl5.createParallelGroup(BASELINE)
.addComponent(find))
.addGroup(gl5.createParallelGroup(BASELINE)
.addComponent(tb))
);
gl5.linkSize(SwingConstants.HORIZONTAL, ftf, ftf1);


This is the code for the ActionListener but i have left empty i cant understand what to do.
I am able to successfully add,remove and update only problem is here.


find.addActionListener(new ActionListener()
{

public void actionPerformed(ActionEvent e)
{
try
{
conn = DriverManager.getConnection("jdbc dbc:addissue", "library","bismillah");
ps = conn.prepareStatement("SELECT * FROM addissue WHERE rollno=?");
ps.setString(1,ftf.getText());
ps.executeQuery();

}
catch(Exception ce)
{
System.out.println("Exception"+ce);
}
}
});
 
satishkumar janakiraman
Ranch Hand
Posts: 334
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will get a quick response if you give us the stack trace. Without seeing the stack trace, it is very difficult to know where the exception is actually thrown.
 
adeeb alexander
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
see this is the code for the layout as well as for the button listener. I have tried to write the code to get data in table. But i dont see any table on the panel except two text fields and a button which i have add below. More Over when i tried to display the table in another panel without any other component it was successfully seen. Tanks for helping me a lot.

ftf = new JTextField(20);
ftf1 = new JTextField(20);
froll = new JLabel("Roll No");
fname = new JLabel("Name");
find = new JButton("Find");
//tb = new JTable();
//ta = new JTextArea(10,10);
GroupLayout gl5 = new GroupLayout(jp5);
jp5.setLayout(gl5);
gl5.setAutoCreateGaps(true);
gl5.setAutoCreateContainerGaps(true);
>>>>>>>>>>>>>>>gl5.setHorizontalGroup(gl5.createSequentialGroup()<<<<<<<<<<<
.addGroup(gl5.createParallelGroup(LEADING)
.addComponent(froll)
.addComponent(fname))
.addGroup(gl5.createParallelGroup(LEADING)
.addComponent(ftf)
.addComponent(ftf1)
.addComponent(find, (CENTER))
.addComponent(tb, (CENTER)))
);
gl5.setVerticalGroup(gl5.createSequentialGroup()
.addGroup(gl5.createParallelGroup(BASELINE)
.addComponent(froll)
.addComponent(ftf))
.addGroup(gl5.createParallelGroup(BASELINE)
.addComponent(fname)
.addComponent(ftf1))
.addGroup(gl5.createParallelGroup(BASELINE)
.addComponent(find))
.addGroup(gl5.createParallelGroup(BASELINE)
.addComponent(tb))
);
gl5.linkSize(SwingConstants.HORIZONTAL, ftf, ftf1);
find.addActionListener(new ActionListener()
{

public void actionPerformed(ActionEvent e)
{
Vector colnames = new Vector();
Vector tbdata = new Vector();
try
{
conn = DriverManager.getConnection("jdbc dbc:addissue", "library","adeeb");
String rollno = ftf.getText();
int roll = Integer.parseInt(rollno);
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM addissue WHERE rollno="+roll);
ResultSetMetaData md = rs.getMetaData();
int col = md.getColumnCount();

for (int i=1;i<=col;i++)
{
colnames.addElement(md.getColumnName(i));
}
while(rs.next())
{
Vector row1 = new Vector(col);
for(int i=1;i<=col;i++)
{
row1.addElement(rs.getObject(i));
}
tbdata.addElement(row1);
}
rs.close();
stmt.close();
conn.close();
tb = new JTable(tbdata,colnames);
JScrollPane jsp1 = new JScrollPane(tb);
}

catch(Exception ce)
{
System.out.println("Exception"+ce);
}
}
});


[ June 09, 2008: Message edited by: adeeb alexander ]

[ June 09, 2008: Message edited by: adeeb alexander ]
[ June 09, 2008: Message edited by: adeeb alexander ]
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reposting with proper code tags as the original poster is probably too busy for such trivia
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!