Hello,

I would like to retrieve the data from one of my database table. I have read many stuff about it and found out that I need to create a table model which is quite confusing to me.

Basically my table consists of customer information. What is the best way to retrieve the data? jtable or jlist? Also any tips how to start.

Can I retrieve data from text field? And have a next button that will go to the next record?
Sorry for all my questions, I have been working in VB before and find it totally different.

Thank you

Here is some code i wrote many years ago.
Retrieving data from an access database and display them using a JTable (my suggestion)
Maybe you will find something useful.

package askhsh3;

/**
 *
 * @author nikos
 */

import javax.swing.table.*;
import java.awt.Component;
import java.awt.Color;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.JTable;
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JFrame;
import java.sql.*;
import java.io.*;

public class airportboard extends JFrame implements ActionListener ,  Runnable{

        public synchronized void database(JTable menu) {
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        
         GridLayout frameGrid = new GridLayout(1, 1);       
         setLayout(frameGrid);
         add(menu);
         remove(menu);
         setVisible(true);
          try{
          Thread.sleep(1000);        
          }catch (InterruptedException e){System.out.println("me diakopsane re gamwto"+ e.getMessage());}
        }    
 public  void run()
          {
      while(true){
     try{
        String data = "jdbc:odbc:bashpthsewn";
                 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                 Connection conn = DriverManager.getConnection(data, "", "");
                 Statement st = conn.createStatement();
                 st.execute("SELECT * FROM flightsjava ");
                 final ResultSet rec = st.getResultSet();
                 int j = 0;
                 String s[] = new String[10];
                 JTable menu = new JTable(48 , 7);
                 menu.setRowHeight(30);
                  TableColumn tcol;
                 int k = 1;
                 String[] columnNames ={"proorismos",
                                  "etaireia",
                                     "pthsh",
                                      "mesw",
                               "anamenwmwnh",
                          "programmatismenh",
                              "parathrhseis"};
                     for (int i=0;i<7;i++){  
                     menu.setValueAt(columnNames[i],0,i);
                      tcol =menu.getColumnModel().getColumn(i);
                   tcol.setCellRenderer(new CustomTableCellRenderer());
                  }
                 while (rec.next()) {
                  for (int i=0;i<7;i++){                     
                     s[i] = rec.getString(i+1);  
                     menu.setRowHeight(30);
                     menu.setValueAt(s[i],k,i);
                     menu.setAutoscrolls(true);
                     menu.setDefaultRenderer(menu.getClass(), new CustomTableCellRenderer());
                    tcol =menu.getColumnModel().getColumn(i);
                    tcol.setCellRenderer(new CustomTableCellRenderer());
                  }
                  k = k+1;  
                  }
                  this.database(menu);
                  conn.close();
         }
         catch (SQLException s) {
                 System.out.println (s.toString()+s.getErrorCode()+s.getSQLState());
               }
        catch (Exception e1) {
                 System.out.println (e1.toString()+e1.getMessage());
               }         
          try{               
          Thread.sleep(500); }catch (InterruptedException e){System.out.println("me diakopsane re gamwto"+ e.getMessage());}
                    }

      }

 public class CustomTableCellRenderer extends DefaultTableCellRenderer
{
        @Override
    public Component getTableCellRendererComponent
       (JTable table, Object value, boolean isSelected,boolean hasFocus, int row, int column)
    {
        Component cell = super.getTableCellRendererComponent
           (table, value, isSelected, hasFocus, row, column);
        if (row % 2 == 0) {
          cell.setBackground(Color.cyan);//diaforetiko xrwma oimones apo tis zyges grammes
        }
        else {
          cell.setBackground(Color.lightGray);//diaforetiko xrwma oimones apo tis zyges grammes
        }
         if (row==0)
        {
            Font font =new Font("Tahoma" , Font.BOLD , 12);
            cell.setFont(font);        
        }              
        if( value.toString().equalsIgnoreCase("Departed" ))

                setFont(font);                

        else{

           if( value.toString().equalsIgnoreCase("Delayed"))
        {

                cell.setForeground( Color.decode("#ffff00") );

            }
      else{
                if( value.toString().equalsIgnoreCase("Cancelled"  ))
        {

                cell.setForeground(Color.decode("#ff0000") );

            }
     else{

         if( value.toString().equalsIgnoreCase("OnBoarding" ))
        {
                cell.setForeground(Color.decode("#0000ff") );


         }
                  else{

         if( value.toString().equalsIgnoreCase("OnTime" ))
        {
                cell.setForeground(Color.decode("#000000") ); 

            }}}}}

        return cell;
    }
}   
    public void actionPerformed(ActionEvent event) {
        try {

        } catch (Exception exc) {
            System.out.println(exc.getMessage());           
        }
    }
     public static void main(String args[]){
      airportboard pinakas = new airportboard();
            pinakas.setSize(1440, 900);
            pinakas.run();}

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.