tags:

views:

80

answers:

2

I have a "Manager" class and "BirthList" frame. In my BirthList frame I have a GUI table that shows all the data that are in mySQL table but it does like this:

When I open this frame, I see the last data that I added and when I click on Close button, at first the last data will be deleted from my table and then the frame will be closed and if I open the frame again, I see whole data which are in MySQL tabel and also I will see the last data for two times. Why?

I want to show my data from MySQL table in my GUI table. If I close my frame and then open that, I want to see all those data that I added before + the new data that I added it recently.

user class:

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 frame:

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);
        }
    }
}
+1  A: 

For your isAddBirth request, it can be replaced by

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

And so on for the other criteria, then in the result set you'll have no result, or only one (supposing you had in your table a way to not duplicate entries according to their name, family name date of birth ...)

Maybe it could be useful for you to name your sql tables and sql rows name with Java variables, so in case of modifications there won't be coherence problems.

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

Then you can simply see if the result set has returned something or not and set up your return value according to this.

Vincent Briard
-1, The poster never accepts answers and will never change her ways unless you stop spoon feeding answers. Just because we are on the web is not excuse for the poster to reply with a simple "thank you" be accepting helpful answers. Anyway you should be using PreparedStatements for SQL so you don't have to worry about SQL syntax as much. Also, there are SQL tutorial you can refer the poster to so she can actually learn something and learn how to customize the SQL on her own.
camickr
The OP not accepting answers is a lousy reason to downvote a valid answer.
Kaleb Brasee
I agree though, the code is very, very sad.
Kaleb Brasee
+2  A: 

Honestly said, I am going to sound harsh, but this is the truth. The posted code is terrible. I know this should have been a comment, but this isn't going to fit in a comment. I just wanted to highly recommend you to take the following steps:

  1. Learn Java here: http://java.sun.com/docs/books/tutorial/
  2. Learn SQL here: http://www.w3schools.com/sql/
  3. Learn JDBC here: http://java.sun.com/docs/books/tutorial/jdbc/
  4. Learn DAO here: http://balusc.blogspot.com/2008/07/dao-tutorial-data-layer.html

From 1. you should learn under each how and when (not) to use static. From 2. you should under each learn how to execute specific queries with help of WHERE clause so that you don't need to haul the whole database contents into Java's memory and looping through it just only to check if something exist. From 3. you should learn under each how to acquire and close resources in a proper manner to avoid resource leaks and how to use PreparedStatement to save your code from SQL Injections. From 4. you should learn under each how to put the complete JDBC picture nicely together.

Hope this helps. Much good luck.

BalusC
Thanks!I will follow these steps:)
Johanna
I wish I could upvote this answer 1,000 times, and 1,000 more times.
delfuego