Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Displaying values from a database in jLabel

807601Mar 6 2008 — edited Mar 7 2008
Hi, I'm new to java and need a slight nudge in the right direction. I have a customer table in my database and I want to display the values of a row of that table in jlabels when a button is pressed. I think I have most of the code in place, but am not really sure how to transfer the data between the different classes in the application.

Firstly, I have a Menu class which contains the button and jlabels, a DatabaseIO class which contains the connection and query etc and a list class for the customer.

Here is the code for the button in the Menu class
private void initGUI() {
		try
							searchCustomerButton = new JButton();
							searchCustomerButton.setText("Search");
							searchCustomerButton.addActionListener(new ActionListener() {
								public void actionPerformed(ActionEvent evt) {
									System.out.println("searchCustomerButton.actionPerformed, event="+evt);
									String i;
									int id;
									i = cusIDSearchField.getText();
									id = Integer.parseInt(i);
									DatabaseIO.getCustomers(id);
This is the code in the DatabaseIO class
public static CustomerList getCustomers(int id){
    	
        String query = 	"SELECT * FROM customer WHERE customerID = '"+id+"'";
        
        CustomerList listOfCustomers = new CustomerList();
//     	System.out.println("setting up list");   
        try{
        	
        	Statement stmt = con.createStatement();
//        	System.out.println("statement created");
        	ResultSet rs = stmt.executeQuery(query);
    		while (rs.next()) {
                String fn = rs.getString("firstName");
                String ln = rs.getString("lastName");
                System.out.println("Name: " + fn + " " + ln);
                String ca = rs.getString("address");
                System.out.println("Address: " + ca + " ");
                String pn = rs.getString("phoneNumber");
                System.out.println("Phone Number: " + pn);
                int dp = rs.getInt("depositPercentage");
                System.out.println("Deposit: " + dp + "%  ");
                double cb = rs.getDouble("budget");
                System.out.println("Extras Budget: ?" + cb);
                System.out.println("");
                Customer tempCustomer = new Customer(fn,ln,ca,pn,dp,cb);
                listOfCustomers.add(tempCustomer);
    		}
    	}
        catch (Exception e){
        	System.out.println("customers error " + e);
        }
        return listOfCustomers;
        }
Here is the code in the list class

public boolean addCustomers(Customer customer) {
		return customers.add(customer);
	}
The query obviously works as all the relevant data gets displayed in the console but can anyone help me work out how to get it to display in the labels on the menu? Thanks in advance.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 4 2008
Added on Mar 6 2008
3 comments
1,444 views