mardi 23 juin 2015

JDBC- Insert a member into a database by GUI

I want a user to fill in data in text form . Based on these text form , the database is updated and a new member should add. My problem is that when you type stuff in the text fields and press the "Add member" nothing happens .

This is what my code currently looks like: what i want to do here is add a member with an actionlistener to save it when i press "ok" and update the database.

        ActionListener l13 = new ActionListener(){ 
        public void actionPerformed(ActionEvent e) {
        int id = 300; 
        String givenName = "givenName";
        String familyName ="familyName";
        String email = "email";
        String gender = "gender";
        String birth = "birth" ;
        String memberSince = "memberSince";
        int active = 1;
        //int role = 0;
        //String team = null;
        //int pid = 0;
        //int cid = 0;
        try{

            //laggtillmedlem()
            id = Integer.parseInt(laggid.getText());
            givenName = laggnamn.getText();
            familyName = laggfamiljnamn.getText();
            email = laggemail.getText();
            gender = laggkon.getText();
            birth = laggbirth.getText();
            memberSince = laggmdatum.getText();
            active = Integer.parseInt(laggaktiv.getText());
            //role = Integer.parseInt(laggroll.getText());
            //team = lagglag.getText();
            //pid = Integer.parseInt(laggpid.getText());
            //cid = Integer.parseInt(laggcid.getText());
        } catch (Exception xe){
            laggid.setText("fel");     
        } 
        try{

            ArrayList<NextMedlem> sqlsvar = databas();


            laggtillmedlem(id, givenName, familyName, email, gender, birth, memberSince, active);
            //uppdateralag

    }   catch(SQLException se){
            System.out.println("SQLException");
    }   catch (ClassNotFoundException ce){
            System.out.println("Class");
    }        

        }
};

This is what the second part of the code looks like connected to the first one.

    public static void laggtillmedlem(int id, String givenName, String familyName, String email, String gender, String birth, String memberSince, int active)throws SQLException, ClassNotFoundException {

try {
    Class.forName("org.sqlite.JDBC");
    ArrayList<NextMedlem> sqlsvar = new ArrayList<NextMedlem>();
    Connection c;
    c = DriverManager.getConnection("jdbc:sqlite:lag");
    Statement  s = c.createStatement();



    String query = "INSERT INTO medlem VALUES (" + id + ", '" + givenName + "', '" + familyName + "', '" + email + "', '" + gender + "', '" + birth + "', '" + memberSince + "', " + active + ")";
    s.executeUpdate(query);

    JOptionPane.showMessageDialog(null,"A member has been added!");

} catch (java.sql.SQLException e) {
    JOptionPane.showMessageDialog(null, "Couldn't add member!");
    System.out.println(e);
} catch(java.lang.NullPointerException f) {
    JOptionPane.showMessageDialog(null, "Couldn't add member!");
    System.out.println(f);
}
} 

Please assist me in this mission (:

Aucun commentaire:

Enregistrer un commentaire