2013-07-18 2 views
0

이것은 제 1 클래스 이름입니다. ConnectionDBClass.java 데이터베이스에서 데이터를 검색 한 후 저장/표시를 JTable에 저장하려고합니다.데이터베이스 테이블에서 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); 
} 


} 

}

이이 클래스에서이 클래스 Initilize에서

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(); 


} 

} 

내 3 클래스 이름 InilizationAndLabelGUIVariables.java입니다 GUI의 코드 내 2 클래스 이름 GUIClass.java이며, 레이블 GUIClass.java 클래스에서 선언 된 변수

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); 

} 

} 

나 나는 이것에 붙어있다.

답변

4
  • JTable (its model)Table from Database는 같은 구조, 데이터 columns에 저장되어 있으며 rows

  • ResultSet 내부 루프 Resultset에서 모든 행 DefaultTableModel

    하는 new Vector<Object> 또는 new Object[] 테이블 모델 또는 JTable.addRow로 추가
  • ResultSetTableModel 이상에 대한 검색 TableFromDatabase ju 휠체어를 재발견하는 것을 피하십시오.

+0

나는 이것을 이해하지만, 당신의 가이드에 따라 코드를 수정하는 방법을 모르기 때문에 필자의 필요에 따라 코드를 변경할 수 있습니까? 나는 너에게 고맙게 여길 것이다. – Programmer

관련 문제