简体   繁体   中英

Can´t add data through Java application to database

I have a crazy problem :DI have a Java application with a Jtable and the show, add, delete and update Buttons. The connection to my database works.

Now when I insert Data into my Textfields and click on submit (add) I can see the entry in my Jtable , but I cant see it in my Database.

This is crazy because I already have data in my Database some simple entries and my JTable is showing the data but when I insert data through the java app I can´t see the entry in my phpmyadmin database to which I am connected..

It´s hard to explain but maybe you can find the problem :/

TABLE VIEW FROM JAVA JTABLE http://i.imgur.com/BrRUyx9.jpg

TABLE VIEW FROM PHPMYADMIN.COM http://i.imgur.com/TIbVzmc.jpg

MY CODE

CONTROLLER CLASS

public void controllActionListenerAddEquipmentSubmitBtn(){
            main.setActionListenerAddEquipmentSubmitBtn(new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    if(arg0.getSource()==main.getButtonAddEquipmentBtn()){
                        EquipmentDAO edao = new EquipmentDAO();
                        try {
                                edao.create(main.addEquipmentSubmit());
                            } catch (SQLException e) {    
                                e.printStackTrace();
                            }
                        }
                    }
                });
            }

MAINFRAME CLASS...(MAIN)

 public Equipment addEquipmentSubmit(){
        equipment = new Equipment();
        equipment.setName(addEquipment.getName());
        equipment.setSection(addEquipment.getSection());
        equipment.setType(addEquipment.getType());
        equipment.setValue(Double.parseDouble((addEquipment.getValue())));
        equipment.setAmount(Integer.parseInt(addEquipment.getAmount()));
        return equipment;    
    }

DAOCLASS

package DAOCollection;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

import com.mysql.jdbc.PreparedStatement;

import notesDBMySQL.DatabaseHandler;
import EntityClasses.Equipment;

public class EquipmentDAO implements DaoInterface{

    private Connection con;
    private java.sql.PreparedStatement sta;
    private Statement std;
    private ResultSet res;
    private DatabaseHandler handle;
    private String query;

    @Override
    public ResultSet show() throws SQLException{    
        handle = new DatabaseHandler();
        Connection con = handle.buildConnectionToServer();
        String query = "select EID as ID, EquipmentName as Name, EquipmentSection as Section, EquipmentType as Type, EquipmentValue as Value, EquipmentAmount as Amount from Equipment";
        sta = con.prepareStatement(query);
        res = sta.executeQuery(query);
        return res;    
    }

    @Override
    public void create(Object c) throws SQLException {    
        handle = new DatabaseHandler();
        con = handle.buildConnectionToServer();
        std = con.createStatement();
        Equipment equ = (Equipment) c ;
        query = "INSERT INTO Equipment(EquipmentName,EquipmentSection,EquipmentType,EquipmentValue, EquipmentAmount)"+"VALUES('"+equ.getName()+"','"+equ.getSection()+"','"+equ.getType()+"','"+equ.getValue()+"','"+equ.getAmount()+"')";
        std.execute(query);
        con.commit();
        handle.closeConnection();      
    }

    @Override    
    public void update(Object c) throws SQLException {    
        Equipment equ = (Equipment) c;
        handle = new DatabaseHandler();
        con = handle.buildConnectionToServer();
        query = "UPDATE Equipment SET  name=?,section=?,type=?,value=?,amount=?,"+"WHERE EID=?";
        sta = con.prepareStatement(query);
        sta.setString(1, equ.getName());
        sta.setString(2,equ.getSection());
        sta.setString(3, equ.getType());
        sta.setDouble(4, equ.getValue());
        sta.setInt(5, equ.getAmount());
        sta.setInt(6, equ.getEid());
        sta.executeUpdate();
        sta.close();
        con.close();
        handle.closeConnection();
    }

    @Override
    public void delete(int id) throws SQLException {    
        handle = new DatabaseHandler();
        con = handle.buildConnectionToServer();
        query = "DELETE FROM EQUIPMENT"+"WHERE EID="+id;
        sta.executeUpdate(query);
        sta.close();
        con.close();
        handle.closeConnection();
    }
}

ENTITY CLASS

package EntityClasses;

public class Equipment {

    private int eid;
    private String name;
    private String section;
    private String type;
    private double value;
    private int amount;

    public int getEid() {
        return eid;
    }

    public void setEid(int eid) {
        this.eid = eid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSection() {
        return section;
    }

    public void setSection(String section) {
        this.section = section;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public double getValue() {
        return value;
    }

    public void setValue(double value) {
        this.value = value;
    }

    public int getAmount() {
        return amount;
    }

    public void setAmount(int amount) {
        this.amount = amount;
    }


}

ADD CLASS

import javax.swing.JPanel;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.ImageIcon;

import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;    

public class AddEquipment extends JPanel {
    private JTextField txtu2;
    private JTextField txtu3;
    private JTextField txtu4;
    private JTextField txtu5;
    private JTextField txtu6;

    private JButton btnu8;
    private JButton btnu9; //Back Button

    public AddEquipment() {
        setLayout(null);

        JLabel lblu8 = new JLabel("Add Equipment");
        lblu8.setForeground(Color.WHITE);
        lblu8.setFont(new Font("Tahoma", Font.BOLD, 16));
        lblu8.setBounds(12, 13, 319, 33);
        add(lblu8);

        JLabel lblu3 = new JLabel("Name");
        lblu3.setFont(new Font("Tahoma", Font.BOLD, 13));
        lblu3.setBounds(60, 91, 46, 14);
        add(lblu3);

        txtu2 = new JTextField();
        txtu2.setColumns(10);
        txtu2.setBounds(221, 88, 377, 22);
        add(txtu2);

        JLabel lblu4 = new JLabel("Section");
        lblu4.setFont(new Font("Tahoma", Font.BOLD, 13));
        lblu4.setBounds(60, 156, 69, 14);
        add(lblu4);

        txtu3 = new JTextField();
        txtu3.setColumns(10);
        txtu3.setBounds(221, 153, 377, 22);
        add(txtu3);

        JLabel lblu5 = new JLabel("Type");
        lblu5.setFont(new Font("Tahoma", Font.BOLD, 13));
        lblu5.setBounds(60, 221, 46, 14);
        add(lblu5);

        txtu4 = new JTextField();
        txtu4.setColumns(10);
        txtu4.setBounds(221, 218, 377, 22);
        add(txtu4);

        JLabel lblu6 = new JLabel("Value");
        lblu6.setFont(new Font("Tahoma", Font.BOLD, 13));
        lblu6.setBounds(60, 286, 46, 14);
        add(lblu6);

        txtu5 = new JTextField();
        txtu5.setColumns(10);
        txtu5.setBounds(221, 283, 377, 22);
        add(txtu5);

        JLabel lblu7 = new JLabel("Amount");
        lblu7.setFont(new Font("Tahoma", Font.BOLD, 13));
        lblu7.setBounds(60, 351, 69, 14);
        add(lblu7);

        txtu6 = new JTextField();
        txtu6.setColumns(10);
        txtu6.setBounds(221, 348, 377, 22);
        add(txtu6);

        btnu8 = new JButton("Submit");
        btnu8.setFont(new Font("Tahoma", Font.PLAIN, 13));

        //btnu8.setBackground(Color.BLACK); // button color
        //btnu8.setBackground( new Color(0, 0, 0, 50) ); //opacity

        btnu8.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

            }
        });

        btnu8.setBounds(422, 534, 110, 30);
        add(btnu8);

        btnu9 = new JButton("Back");
        btnu9.setFont(new Font("Tahoma", Font.PLAIN, 13));
        btnu9.setBounds(542, 534, 110, 30);
        add(btnu9);

        JLabel lblback3 = new JLabel("");
        lblback3.setIcon(new ImageIcon("\\\\I-INTRA-03\\IIS-Lehre\\DAPRO-WF4.informatik.hs-ulm.de\\15\\group_together\\GUI_graphics\\background_panel.jpg"));
        lblback3.setBounds(0, 0, 664, 577);
        add(lblback3);  
    }

    //Getter-Method for the Back Button "btnu9"
    public JButton getBackAddEquipment(){
        return btnu9;   
    }

    //Getter-Method for the "Submit" Button "btnu8"
    public JButton getAddEquipmentBtn(){
            return btnu8;   
    }

    public String getName() {
        return this.txtu2.getText();
    }

    // method sets the string value of the textfield
    public void setName(String txtu2) {
        this.txtu2.setText(txtu2);
    }

    public String getSection() {
        return this.txtu3.getText();
    }

    public void setSection(String txtu3) {
        this.txtu3.setText(txtu3);
    }

    public String getType() {
        return this.txtu4.getText();
    }

    public void setType(String txtu4) {
        this.txtu4.setText(txtu4);
    }

    public String getValue() {
        return this.txtu5.getText();

    }

    public void setValue(String txtu5) {
        this.txtu5.setText(txtu5);
    }

    public String getAmount() {
        return this.txtu6.getText();
    }

    public void setAmount(String txtu6) {
        this.txtu6.setText(txtu6);
    }
}

Your DAO implementation is asking for trouble. You should never, ever return a ResultSet . A better idea is to load the data into an object or data structure and close the ResultSet in method scope in a finally block. Same for all your database resources.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM