2013-03-25 95 views
0

我试图创建一个表并通过使用java swing中的表将值添加到sql。但是在for循环中我根本没有递增。我认为它是因为while(rs2.next())。当我把它放在for循环中时,我得到这个错误:“在结果集开始之前”。我需要帮助!JTable不更新

编辑:上面那些问题处理,但我现在有新的问题。这看起来像JTable不更新自己。当我在组合框表应用程序崩溃中选择一个表名并给出数组索引超出界限例外。

我的代码是在这里:

public class DBC extends JFrame{ 

static String tablo; 
static JTextField tf = new JTextField(20); 
static int columnCount; 
static JPanel tfPanel = new JPanel(); 
static String[] sutunlar; 
static JLabel sutunLabel; 
static JPanel sutunPanel = new JPanel(new BorderLayout()); 
static JTable table; 
static JScrollPane scrollPane; 

public static void main(String[] args) throws Exception { 

    Class.forName("com.mysql.jdbc.Driver"); 
    Connection connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/project" 
       ,"root","123456789"); 

    final Statement statement = connect.createStatement(); 

    JLabel tabloSec = new JLabel("Tablo Seçin:"); 
    final JComboBox<String> tablolar = new JComboBox<String>(); 
    final DatabaseMetaData md = connect.getMetaData(); 
    final ResultSet rs = md.getTables(null, null, "%", null); 

    while (rs.next()) { 
     tablolar.addItem(rs.getString(3)); 
    } 

    tablolar.addActionListener(new ActionListener(){ 

     @Override 
     public void actionPerformed(ActionEvent arg0) { 

      tablo = tablolar.getSelectedItem().toString(); 

      try { 

       ResultSet rs2 = statement.executeQuery("SELECT * FROM "+tablo); 
       ResultSetMetaData rsmd = rs2.getMetaData(); 
       columnCount = rsmd.getColumnCount(); 

       sutunlar = new String[columnCount]; 
       table = new JTable(1,columnCount); 
       Object columnNames[] = new Object[columnCount]; 
       Object rowData[][] = {{""}}; 

        for(int i=0;i<columnCount;i++){ 

         sutunlar[i] = rsmd.getColumnLabel(i+1); 
         columnNames[i] = sutunlar[i]; 

         } 

        table = new JTable(rowData, columnNames); 
        table.repaint(); 
        scrollPane = new JScrollPane(table); 
        sutunPanel.add(scrollPane); 
        sutunPanel.revalidate(); 

      } catch (SQLException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
    }); 

    JButton ekle = new JButton("Ekle"); 
    ekle.addActionListener(new ActionListener(){ 

     @Override 
     public void actionPerformed(ActionEvent e) { 

      try { 
       switch(tablo){ 
       case "department": 

        statement.executeUpdate("INSERT INTO department(Name,Location) VALUES('"+tf.getText()+"')"); 
       case "employee": 

        statement.executeUpdate("INSERT INTO employee(Id,FirstName,LastName,Sex,Address,Email,Salary,BirthDate,JoinDate) VALUES('"+tf.getText()+"')"); 
       case "engineer": 

        statement.executeUpdate("INSERT INTO engineer(EngineerType) VALUES('"+tf.getText()+"')"); 
       case "manager": 

        statement.executeUpdate("INSERT INTO manager(Department) VALUES('"+tf.getText()+"')"); 
       case "project": 

        statement.executeUpdate("INSERT INTO project(Name,Number,Value) VALUES('"+tf.getText()+"')"); 
       case "secretary": 

        statement.executeUpdate("INSERT INTO secretary(TypingSpeed) VALUES('"+tf.getText()+"')"); 
       } 

      } catch (SQLException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 
     } 
    }); 

    JButton cik = new JButton("Çık"); 
    cik.addActionListener(new ActionListener() { 

     @Override 
     public void actionPerformed(ActionEvent e) { 

      System.exit(0); 
     } 
    }); 

    JPanel panel = new JPanel(new FlowLayout()); 

    panel.add(tabloSec); 
    panel.add(tablolar); 
    panel.add(sutunPanel); 
    panel.revalidate(); 
    panel.add(ekle); 
    panel.add(cik); 

    JFrame frame = new JFrame("Deneme"); 
    frame.setSize(600,600); 
    frame.setLocationRelativeTo(null); 
    frame.add(panel); 
    frame.setVisible(true); 
    } 
} 
+0

为什么你在循环的列数?用'while'你已经经历了所有返回的行。 – 2013-03-25 14:22:26

+0

我想要获取特定表中的列名。所以,如果该表有3列,我必须进入循环3次。我错了吗? – Miral 2013-03-25 14:24:45

+0

columnCount的值是多少? – 2013-03-25 14:24:51

回答

0

我不认为有问题的for循环。

while(rs2.next()){ 

} 

当一个条件对于我的其他迭代而言是错误的,并且它不输入while循环时,这个条件被执行。

如果你想测试你可以这样做:

for(int i=0;i<columnCount;i++){ 

    while(rs2.next()){    

     sutunlar[i] = rs2.getString(4); 
     Set<String> setSutunlar = new HashSet<>(Arrays.asList(sutunlar[i])); 
     Object rowData[][] = {null}; 
     Object columnNames[] = setSutunlar; 
     table = new JTable(rowData, columnNames); 
     scrollPane = new JScrollPane(table); 
     sutunPanel.add(scrollPane); 
    } 

    System.out.println("Value of i is " + i);// Add this line to check iteration of i. 
} 
+0

这是我编写之前,我写在这里。我在调试模式下检查了我,当它进入循环时,我再次变为0。 – Miral 2013-03-25 14:43:57

+0

@Miral怎么可能。我认为你的while循环只有在i的值为零后执行一次,rs.next()就变成了fales,它不会在while循环中进入。 – 2013-03-25 14:49:57

0

我觉得你混合的东西了。当你定义

columnCount = rsmd.getColumnCount(); 

看来你期望columnCount持有该表的列数;但这种情况并非如此。这是元数据提供的列数。

如果你想获取选定表的列的名称,把这个在您的try/catch块:

ResultSet rs2 = md.getColumns(null, null, tablo, null); 

Set<String> columnNames = new HashSet<String>(); 
while (rs2.next()) { 
    columnNames.add(rs2.getString(4)); 
} 

Object[][] rowData = { null }; 
table = new JTable({ null }, columnNames.toArray()); 
scrollPane = new JScrollPane(table); 
sutunPanel.add(scrollPane); 

而且你真的应该想想你的静态字段的使用;)