2009-12-15 70 views
-1

我有一个“经理”类和“BirthList”框架。在我的BirthList框架中,我有一个显示mySQL表中所有数据的GUI表,但它的确如此:为什么我不能在我的GUI表中显示我的MySQL信息?

当我打开这个框架时,我看到最后添加的数据,当我点击关闭按钮,首先最后的数据将从我的表中删除,然后框架将被关闭,如果我再次打开框架,我会看到在MySQL tabel中的整个数据,我也会看到最后两次数据。为什么?

我想表明我的GUI表从MySQL表我的数据。如果我关闭框架然后打开它,我希望看到之前添加的所有那些数据+我最近添加的新数据。

用户类:

public class Manager { 
    Logger logger = Logger.getLogger(this.getClass().getName()); 
    private static Connection conn = DBManager.getConnection(); 
    public static Admin admin; 
    private static List<Birth> birthList; 

    public static void addBirth(String name, String family, String fatherName, String mName, String dOfBirth, String pOfBirth) { 
     try { 
      Statement stmt = conn.createStatement(); 
      stmt.executeUpdate("INSERT INTO birthtable(name,family,fatherName,motherName,dateOfBirth,placeOfBirth)" + "VALUES('" + name + "','" + family + "','" + fatherName + "','" + mName + "','" + dOfBirth + "','" + pOfBirth + "')"); 
     } catch (SQLException ex) { 
      Logger.getLogger(Manager.class.getName()).log(Level.SEVERE, null, ex); 
     } 

    } 

    public static boolean isAddBirth(String name, String family, String fatherName, String mName, String dOfBirth, String pOfBirth) { 
     boolean bool = true; 
     Statement stmt = null; 
     try { 
      stmt = conn.createStatement(); 
     } catch (SQLException ex) { 
      Logger.getLogger(Manager.class.getName()).log(Level.SEVERE, null, ex); 
     } 
     ResultSet rst = null; 
     try { 
      rst = stmt.executeQuery("SELECT * FROM birthtable"); 
     } catch (SQLException ex) { 
      Logger.getLogger(Manager.class.getName()).log(Level.SEVERE, null, ex); 
     } 
     try { 
      while (rst.next()) { 
       if (rst.getString(2).equals(name) && rst.getString(3).equals(family) && rst.getString(4).equals(fatherName) && rst.getString(5).equals(mName) && rst.getString(6).equals(dOfBirth) && rst.getString(7).equals(pOfBirth)) { 
        bool = false; 
       } else { 
        bool = true; 
       } 
      } 
     } catch (SQLException ex) { 
      Logger.getLogger(Manager.class.getName()).log(Level.SEVERE, null, ex); 
     } 
     return bool; 
    } 

    public static void addToBirthListFromMySQL() throws SQLException { 
     Birth list1 = null; 
     Statement stmt = conn.createStatement(); 
     ResultSet rs = stmt.executeQuery("SELECT * FROM birthtable"); 

     while (rs.next()) { 
      String s1 = rs.getString(2); 
      if (rs.wasNull()) { 
       s1 = null; 
      } 
      String s2 = rs.getString(3); 
      if (rs.wasNull()) { 
       s2 = null; 
      } 
      String s3 = rs.getString(4); 
      if (rs.wasNull()) { 
       s3 = null; 
      } 
      String s4 = rs.getString(5); 
      if (rs.wasNull()) { 
       s4 = null; 
      } 
      String s5 = rs.getString(6); 
      if (rs.wasNull()) { 
       s5 = null; 
      } 
      String s6 = rs.getString(7); 
      if (rs.wasNull()) { 
       s6 = null; 
      } 
      list1 = new Birth(s1, s2, s3, s4, s5, s6); 
      birthList = admin.getBirthList(); 
      if (birthList == null) { 
       birthList = new ArrayList<Birth>(); 
      } 
      birthList.add(list1); 
     } 
     admin.setBirthList(birthList); 
    } 
} 

BirthList帧:

public class BirthList extends javax.swing.JFrame { 

    private Admin admin; 
    List<Birth> list; 
    DefaultTableModel model; 

    /** Creates new form BirthList */ 
    public BirthList(Admin admin) { 
     initComponents(); 
     this.admin = admin; 
     Manager.admin = admin; 
     try { 
      Manager.addToBirthListFromMySQL(); 
     } catch (SQLException ex) { 
      Logger.getLogger(BirthList.class.getName()).log(Level.SEVERE, null, ex); 
     } 
     fillTable(); 
    } 

    private void cancleBActionPerformed(java.awt.event.ActionEvent evt) {           
     for(int i=0;i<jTable1.getRowCount();i++){ 

      model.removeRow(i); 
      model.fireTableDataChanged(); 
     } 

     int r = JOptionPane.showConfirmDialog(this, "Are you sure?", "Message", JOptionPane.YES_NO_CANCEL_OPTION); 
     if(r==JOptionPane.YES_OPTION) 
      this.dispose(); // TODO add your handling code here: 
    }  

    public void fillTable() { 
     String[] columNames = {"name", "family", "father's name", "mother's name", "date of birth", "place of birth"}; 
     List<Birth> birth = admin.getBirthList(); 
     if (birth==null) { 
      JOptionPane.showMessageDialog(this, "Death list is empty! at first ,add a person.", "Error", JOptionPane.ERROR_MESSAGE); 
     }else{ 
      Object[][] data = new Object[birth.size()][columNames.length]; 
      for (int i = 0; i < data.length; i++) { 
       Birth birth1 = birth.get(i); 

       data[i][0] = birth1.getName(); 
       data[i][1] = birth1.getFamily(); 
       data[i][2] = birth1.getFatherName(); 
       data[i][3] = birth1.getMotherName(); 
       data[i][4] = birth1.getDateOfBirth(); 
       data[i][5] = birth1.getPlaceOfBirth(); 
      } 
      model = new DefaultTableModel(data, columNames); 
      jTable1.setModel(model); 
     } 
    } 
} 
+0

你的函数“isAddBirth”的那种,怎么说...为什么没有做一个真正的SQL请求而不是大量迭代与结果集?至少在找到结果集时添加一个中断... – 2009-12-15 04:23:21

+0

我该怎么做? – Johanna 2009-12-15 04:29:33

+0

你能给我举个例子吗? – Johanna 2009-12-15 04:30:07

回答

1

为了您isAddBirth请求时,它可以通过

SELECT * 
FROM birthtable 
WHERE fatherName LIKE \"pFatherName\" 
AND name LIKE \"pName\"; 

等等的其它标准,则替换在结果集中你不会有结果,或者只有一个结果(假设你在表中有一个不重复条目的方法根据他们的名字,姓氏的出生日期...)

也许它可能是有用的你的名字你的sql表和SQL行名称与Java变量,所以在修改的情况下不会有一致性问题。

final String birthTable = "birthtable"; 
final String fatherName = "fatherName"; 

然后你可以简单地看看结果集是否已经返回了一些东西,并根据这个设置你的返回值。

+1

虽然我同意,但代码非常非常难过 – 2009-12-15 05:01:22

2

老实说说,我要听起来刺耳,但就是这个道理。发布的代码是可怕的。我知道这应该是一个评论,但这不适合评论。我只是想强烈建议您采取以下步骤:

  1. 这里学习Java:http://java.sun.com/docs/books/tutorial/
  2. 了解SQL这里:http://www.w3schools.com/sql/
  3. 这里了解JDBC:http://java.sun.com/docs/books/tutorial/jdbc/
  4. 这里了解DAO:http://balusc.blogspot.com/2008/07/dao-tutorial-data-layer.html

从1.你应该学习如何以及何时(不是)使用static。从2.你应该学习如何在WHERE条款的帮助下执行特定的查询,这样你就不需要将整个数据库内容拖入Java的内存中,并循环遍历它,只是为了检查是否存在某些内容。从3开始,您应该了解如何以合适的方式获取和关闭资源,以避免资源泄漏,以及如何使用PreparedStatement从SQL注入中保存代码。从4.你应该学习如何把完整的JDBC图片很好地结合在一起。

希望这会有所帮助。祝你好运。

+0

谢谢!我会按照以下步骤操作: ) – Johanna 2009-12-15 05:12:12

+0

我希望我可以upvote这个答案1,000倍,1,000多次。 – delfuego 2009-12-15 15:00:30

相关问题