【问题标题】:Why I can not show my MySQL information in my GUI table?为什么我不能在我的 GUI 表中显示我的 MySQL 信息?
【发布时间】:2009-12-15 04:13:04
【问题描述】:

我有一个“Manager”类和“BirthList”框架。在我的 BirthList 框架中,我有一个 GUI 表,它显示了 mySQL 表中的所有数据,但它确实像这样:

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

我想在我的 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);
    }
}

出生列表框架:

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

【问题讨论】:

  • 你的函数“isAddBirth”有点,怎么说......为什么不做一个真正的SQL请求而不是对结果集进行多次迭代?至少在找到结果集时添加一个中断...
  • 你能给我举个例子吗?
  • 你还没有接受昨天和前天和前天的答案.....为什么要继续帮助那些不欣赏我们花费的时间和精力的人?
  • 我会感谢您的时间和精力,但我真的连接不好,我只能在一个页面,如果我关闭此页面,我无法轻松打开它,我总是使用刷新页:((
  • 你的连接工作得很好,可以提出问题,所以它工作得很好,可以阅读答案并接受答案。

标签: java mysql user-interface


【解决方案1】:

老实说,我会听起来很苛刻,但这是事实。发布的代码是糟糕。我知道这应该是评论,但这不适合评论。我只是想强烈建议您采取以下步骤:

  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图片很好地组合在一起。

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

【讨论】:

  • 我希望我可以为这个答案投票 1000 次,再投票 1000 次。
【解决方案2】:

对于您的 isAddBirth 请求,可以将其替换为

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

对于其他条件,依此类推,那么在结果集中你将没有结果,或者只有一个(假设你的表中有一种方法可以根据他们的名字,姓氏出生日期不重复条目。 ..)

也许用 Java 变量命名 sql 表和 sql 行名称可能对您很有用,这样在修改时不会出现连贯性问题。

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

那么你就可以简单的看结果集有没有返回一些东西,根据这个设置你的返回值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-08
    • 1970-01-01
    • 1970-01-01
    • 2018-12-07
    • 1970-01-01
    • 2016-09-08
    • 1970-01-01
    相关资源
    最近更新 更多