Good day everyone, I'm trying to save an inputted data using JTextfield in JAVA GUI into MySQL database.
But when i pressed the save button nothing happens. I want to show you all my work right now.
Please try to help me, Thank you in advance!

if(e.getSource().equals(save))  { //THE SAVE BUTTON

String ID=CustAcctNum.getText();
int AcctNum=Integer.parseInt(ID);

String fname=CustFName.getText();
String lname=CustLName.getText();
String tel=CustTelNum.getText();
String city=CustCity.getText();
String zcode=CustZCode.getText();
String status=CustStat.getText();
String creditcard=CreditCNum.getText();
String order=CustOrDt.getText();
String dfee=CustDFee.getText();
String inumber=MnuINum.getText();
String icost=MnuCost.getText();
String tcost=TotalCost.getText();

try {
rs.moveToInsertRow();

rs.updateInt(ID, AcctNum);
rs.updateString("CustFName", fname);
rs.updateString("CustLName", lname);
rs.updateString("CustTelNum", tel);
rs.updateString("CustCity", city);
rs.updateString("CustZCode", zcode);
rs.updateString("CustStat", status);
rs.updateString("CreditCNum", creditcard);
rs.updateString("CustOrDt", order);
rs.updateString("CustDFee", dfee);
rs.updateString("MnuINum", inumber);
rs.updateString("MnuCost", icost);
rs.updateString("TotalCost", tcost);

rs.insertRow();
st.executeQuery("INSERT INTO pizzadb.ordertable VALUES (ID,'fname','lname','tel'" +
        "'city','zcode','status','creditcard','order','dfee','inumber','icost','tcost')");
st.close();
rs.close();


}catch(SQLException err){
    JOptionPane.showMessageDialog(null, err.getMessage());
}}

Recommended Answers

All 10 Replies

I think this should help you JDBC Connection and also you have another way of doing this, you can use URLConnection, i like more this way i dont know why :)

Are you getting a sql exception?
There is no need for ResultSet here. The sql insert should take care of everything you need.

I'm already connected to mysql database, Do i need to remove the resultset?

Yes, try removing it. You are attempting to add the data with resultSet and also with a update query statement. Do one or the other, not both.
If the code is not working, it should be throwing a SQL Exception. What exactly does the exception say? Instead of sending the exception to a JOptionPane, you might be better off using printStackTrace() on it. Then you will at least know exactly which line of code is throwing the exception.

Think this is my errors,

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at BambinosPizzasToGo.PizzaClass.actionPerformed(PizzaClass.java:302)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

Now that's just silly!

java.lang.NumberFormatException: For input string: ""

One of the text fields was blank and your code tried to do an Integer.parseInt on it. Make sure you fill in all the relelvant text fields and make sure the code is processing the data from the text fields properly.

try {

        Class.forName("com.mysql.jdbc.Driver");

        //Abrindo a conexão: ATENÇÃO OS DOIS PARÂMETROS VAZIOS("") SÃO USUÁRIO E SENHA, RESPECTIVAMENTE.
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/pet", "root", "edson");

        //Executa a query de inserção
        java.sql.Statement st = conn.createStatement();
        st.executeUpdate("INSERT INTO cliente VALUES ('"+this.TFcpf.getText() + "','"
                 + this.TFnome.getText() + "','"
                 + this.TFendereco.getText() + "','"
                 + this.TFbairro.getText() + "','"
                 + this.TFcidade.getText() + "','"
                 + this.TFcelular.getText() + "','"
                 + this.TFfone.getText() + "','"
                 + this.Anotas.getText() + "','"
                 + this.TFemail.getText() + "')");
        JOptionPane.showMessageDialog(rootPane, "CLIENTE inserido");
    } 
     catch (SQLException | ClassNotFoundException e) {
        JOptionPane.showMessageDialog(rootPane, e);
    }
}          

Nice try Edson, but this thread is a year old, so your answer is too late.
You should also update your Java knowledge. Class.forName() became osolete with JDBC4, ten years ago!

Try to put the SQL-statement, that you are sending to the MySQL database at line #7 on a MySQL-prompt, and see if it returns any errors, and if you get the correct values in the database.

I'm not an expert in JAVA, but this SQL seems not correct for what you want to achieve:
INSERT INTO thepizzadb.orderstable VALUES (ID,'fname','lname','tel'","'city','zcode','status','creditcard','order','dfee','inumber','icost','tcost')"

richardsonka: please read the previous post, you're about a year late with your response.

Also: use code formatting it makes it easier to read.
Besides: why would one have to be a Java expert to be able to write sql code?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.