Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么我不能在GUI表中显示我的MySQL信息?_Java_Mysql_User Interface - Fatal编程技术网

Java 为什么我不能在GUI表中显示我的MySQL信息?

Java 为什么我不能在GUI表中显示我的MySQL信息?,java,mysql,user-interface,Java,Mysql,User Interface,我有一个经理类和出生名单框架。在我的出生列表框架中,我有一个GUI表,显示mySQL表中的所有数据,但它是这样做的: 当我打开这个框架时,我看到了我添加的最后一个数据,当我单击Close按钮时,首先最后一个数据将从我的表中删除,然后框架将被关闭,如果我再次打开框架,我将看到MySQL选项卡中的全部数据,并且我将看到最后一个数据两次。为什么? 我想在GUI表中显示MySQL表中的数据。如果我关闭框架,然后打开它,我希望看到我之前添加的所有数据+我最近添加的新数据 用户类别: public clas

我有一个经理类和出生名单框架。在我的出生列表框架中,我有一个GUI表,显示mySQL表中的所有数据,但它是这样做的:

当我打开这个框架时,我看到了我添加的最后一个数据,当我单击Close按钮时,首先最后一个数据将从我的表中删除,然后框架将被关闭,如果我再次打开框架,我将看到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请求,它可以替换为

SELECT * 
FROM birthtable
WHERE fatherName LIKE \"pFatherName\"
AND name LIKE \"pName\";
以此类推,对于其他条件,那么在结果集中,您将没有结果,或者只有一个假设,假设您的表中有一种方法,可以根据条目的名称、姓氏和出生日期来避免重复条目

也许用Java变量命名sql表和sql行名称对您很有用,这样在修改时就不会出现一致性问题

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

然后,您可以简单地查看结果集是否返回了某些内容,并根据此设置您的返回值。

老实说,我听起来很严厉,但这是事实。张贴的代码很糟糕。我知道这应该是一个评论,但这不适合作为一个评论。我只是强烈建议您采取以下步骤:

在这里学习Java: 在这里学习SQL: 在这里学习JDBC: 在这里学习道: 从1。您应该在每一项下学习如何以及何时不使用静态。从2。您应该在每个下面学习如何借助WHERE子句执行特定的查询,这样您就不需要将整个数据库内容拖到Java内存中,并在其中循环,而只是为了检查是否存在某些内容。从3。您应该在每一节中学习如何以适当的方式获取和关闭资源以避免资源泄漏,以及如何使用PreparedStatement从SQL注入中保存代码。从4。您应该学习如何将完整的JDBC图片很好地组合在一起


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

你的函数是“出生”有点,怎么说。。。为什么不执行一个真正的SQL请求,而不是使用结果集进行多次迭代?当你的结果集被发现时,至少在其中添加一个中断…你能给我举个例子吗?你仍然没有接受昨天、前一天和前一天的答案。。。。。为什么我们要继续帮助那些不感谢我们花时间和精力帮助的人呢?我会感谢你的时间和精力,但是我的网络连接很差,我只能呆在一个页面上,如果我关闭这个页面,我无法轻松打开它,我经常使用刷新页面:你的连接可以很好地提出问题,因此它可以很好地阅读答案并接受答案。我希望我能把这个答案提高1000次,再提高1000次。