Results 1 to 2 of 2
Thread: jdbc/ swing code problem
- 08-10-2007, 01:53 AM #1
Member
- Join Date
- Aug 2007
- Posts
- 1
- Rep Power
- 0
jdbc/ swing code problem
jdbc/swing
============
Following is my code to retrive data from my ms-access table NAME to display the employee name and his id.
But iam getting the error as
can not resolve the symbol varaible addbutton
in the line (e.getSource == addbutton)
also can not resolve symbol
variable rs ( which is my Resultset that i have created in my code).
can any body help in solving my problem to retrieve and display the values from the table to the textfields.
Thanks in advance.
code starts:
========================================
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class sample1 extends JFrame implements ActionListener
{
public sample1()
{
super("Sample application");
// JPanel p = new JPanel();
JLabel empid = new JLabel("Employee id");
JLabel empnamelabel = new JLabel("Employee name");
JTextField id = new JTextField(20);
JTextField empname = new JTextField(20);
JButton addbutton = new JButton("Add");
Container c = getContentPane();
// c.add(p);
c.add(addbutton);
c.add(empnamelabel);
c.add(empname);
c.add(id);
c.add(empid);
addbutton.addActionListener( new ActionListener);
add( addbutton );
//load the JDBC-ODBC bridge driver
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}
// Get the connection
Connection conn = DriverManager.getConnection("jdbc:odbc:empdsn");
// create a statement object, resultset object and prepared statement to pass sql queries.
Statement st = conn.createStatement();
st.executeQuery("Select * from NAME");
ResultSet rs = st.getResultSet();
pack();
setVisible(true);
}
public static void main(String args[])
{
sample1 sam = new sample1(sam);
sam.addWindowListener(
new WindowAdapter() {
public void windowClosing( WindowEvent e ) {
System.exit( 0 );
}
}
);
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==addbutton) {
try
{
while(rs.next())
{
id.setText(rs.getString(1));
empname.setText(rs.getString(2));
}
}
catch(Exception ee) {}
}
}
}
- 08-13-2007, 02:26 PM #2
Member
- Join Date
- Jul 2007
- Location
- England, Bath
- Posts
- 47
- Rep Power
- 0
From a very quick look it would appear your first major problem is in the creation of addButton:
The ActionListener is an interface and therefore you need to either provide a class which implements the interface in it's place or provide the implementation that you want for the ActionListener in place :Java Code:addbutton.addActionListener([COLOR="DarkRed"] new ActionListener[/COLOR]); add( addbutton );
That will get you past the first hurdle but you have some other very serious issues with your code.Java Code:addbutton.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent arg0) { //TODO Replace this with your actual implementation System.out.println("You have clicked add"); }}); add( addbutton );
I would suggest you grab yourself a basic java book and work through that.
Shane.
Similar Threads
-
Jdbc problem
By abhiN in forum JDBCReplies: 1Last Post: 03-21-2008, 05:10 AM -
EventHandling code for Swing application
By Java Tip in forum Java TipReplies: 0Last Post: 12-07-2007, 12:06 PM -
code for making a java swing program a demo verson
By fakhruddin in forum AWT / SwingReplies: 1Last Post: 11-27-2007, 08:54 PM -
JDBC problem
By Swamipsn in forum New To JavaReplies: 1Last Post: 08-13-2007, 06:05 PM -
JDBC problem
By peiceonly in forum Threads and SynchronizationReplies: 2Last Post: 08-03-2007, 01:42 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks