0

This is my 1st class name ConnectionDBClass.java I want to save retrieve the data from the database and then store/show into JTable.

public class ConnectionDBClass implements Job {

public void execute(JobExecutionContext arg0) throws JobExecutionException {
String serverName = "192.168.0.1";
String portNumber = "1521";
String sid = "hifi";
String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
String username = "courser_hotline";
String password = "courser_hotline";
String[] columnNames = {"command_name", "omc_name", "to_module", "start_time", "end_time", "status", "priority", "cmd_id"};

try {  
    DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());  
//    System.out.println("Connecting to the database...");  
    Connection connection = DriverManager.getConnection(url, username, password);  
    Statement statement = connection.createStatement();  
    String query = "select command_name, omc_name, to_module, start_time, end_time, status, priority, cmd_id from sync_task_table";
//    String query = "select * from sync_task_table";
    ResultSet resultset = statement.executeQuery(query);


  // Create some data
    String dataValues[][] =
  {
    { resultset.getString("command_name"), resultset.getString( "omc_name"), resultset.getString( "to_module"), resultset.getString("start_time"),resultset.getString("end_time"),resultset.getString("status"),resultset.getString("priority"),resultset.getString("cmd_id") }

  };
  //
  //// Create a new table instance
 GUIClass.table = new JTable( dataValues, columnNames );
  }



catch (Exception e) {  
    System.out.println("The exception raised is:" + e);  
}


} 

}

This is my 2nd class name GUIClass.java In this Class the code of GUI

public class GUIClass extends JFrame {
static JFrame frame;
static JLabel formlabel;
static JPanel panel1;
static JPanel panel2;
static JTextField t1;
static JComboBox selectOMC;
static JButton run;
static JPanel panel3;
static DefaultTableModel model;
public static JTable table;
static JScrollPane tableScroll;
static Dimension tablePreferred;


public static void createGUI()
{
    InilizationAndLabelGUIVariables.Initilization();
    InilizationAndLabelGUIVariables.LabelForm();



}

public static void main(String[] args)
{
    createGUI();


}

}

My 3rd class name is InilizationAndLabelGUIVariables.java In this class Initilize and label the variable that are declared in the GUIClass.java class

 public class InilizationAndLabelGUIVariables {

public static void Initilization()
{
    GUIClass.frame = new JFrame("Syncronization Optimizer");
    GUIClass.panel1 = new JPanel(new BorderLayout(5,5));

    GUIClass.panel2 = new JPanel(new FlowLayout(FlowLayout.RIGHT, 3,3));
    GUIClass.formlabel = new JLabel("Syncronization Optimizer Tool");

    GUIClass.t1 = new JTextField(23);
    GUIClass.selectOMC = new JComboBox(ConnectionDB.OMCName);
    GUIClass.run = new JButton("  Run  ");
    GUIClass.panel2 = new JPanel();
    GUIClass.model = new DefaultTableModel(ConnectionDBClass.dataValues, ConnectionDBClass.columnNames);
    GUIClass.table = new JTable(GUIClass.model);
    try {
        // 1.6+
        GUIClass.table.setAutoCreateRowSorter(true);
    } catch(Exception continuewithNoSort) {
    }
    GUIClass.tableScroll = new JScrollPane(GUIClass.table);
    GUIClass.tablePreferred = GUIClass.tableScroll.getPreferredSize();
    GUIClass.tableScroll.setPreferredSize(
        new Dimension(GUIClass.tablePreferred.width, GUIClass.tablePreferred.height/3) );


    GUIClass.frame.setContentPane(GUIClass.panel1);

    GUIClass.frame.setBounds(150,100,570,491);
    GUIClass.frame.setResizable(false);
    GUIClass.frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    GUIClass.frame.setVisible(true);
}


public static void LabelForm()
{
     GUIClass.panel1.add(GUIClass.panel2, BorderLayout.NORTH);
     GUIClass.panel2.add(GUIClass.formlabel);
     GUIClass.panel2.add(GUIClass.selectOMC);
     GUIClass.panel1.add(GUIClass.tableScroll);
     GUIClass.panel2.add(GUIClass.table);
    // gui.add( splitPane, BorderLayout.CENTER );

}

}

Please help me I am stuck do to this.

1 Answer 1

4
  • JTable (its model) and Table from Database has the same structure, data are stored in columns and rows

  • loop inside ResultSet and every row from Resultset to add as a new Vector<Object> or new Object[] to table model or JTable.addRow for DefaultTableModel

  • serch for ResultSetTableModel or better for TableFromDatabase just avoiding to reinvent the wheel

Sign up to request clarification or add additional context in comments.

1 Comment

bro i understand this but can you make changes in my code according to to need and send me the final because I don't know how to modify my code according to your guide. I shall be very thankful to you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.