-2

I'm doing a simple java application in Netbeans consisting on a registration form with 2 fields, name and age. I want to submit the textfield information to a MySQL database. I'm using the following code, when you click the Submit button:

private void btnOKActionPerformed(java.awt.event.ActionEvent evt)
{                                      

    String name = txtName.getText();
    String age = txtAge.getText();

    String request = "INSERT INTO register VALUES ('"+name+"','"+age+"');";


    try{
    Class.forName("com.mysql.jdbc.Driver");
    Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/registsDB","root","root");
    Statement st = conn.createStatement();
    st.executeQuery(request);
    }
    catch(Exception ex) {
        System.out.println("Whoops.");
    }

I think the connection is sucessful, but then there's some sort of error while executing the query.I've seen several examples and they use similar code, can't figure out what's wrong.

Fairly new to java. I'd be so grateful if someone could help me. Thanks in advance.

Edit:

List of errors:

java.sql.SQLException: Can not issue data manipulation statements with executeQuery().
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:974)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919)
at com.mysql.jdbc.StatementImpl.checkForDml(StatementImpl.java:504)
at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1523)
at jantarinho.Cadastro.btnOKActionPerformed(Register.java:113)
at registsDB.Register.access$000(Register.java:19)
at registDB.Register$1.actionPerformed(Register.java:52)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
user207421
  • 305,947
  • 44
  • 307
  • 483
Hugo P
  • 31
  • 6
  • 1
    What sort of errors are you getting? – CBredlow Nov 20 '14 at 02:00
  • Please include the full error message in your question. – Jason D Nov 20 '14 at 02:04
  • Change your catch statement to ex.printStackTrace() and see what the error is. Do not swallow exceptions. – Vincent Ramdhanie Nov 20 '14 at 02:04
  • It can't get past the try statement. If I comment st.executeQuery(request) it does pass. – Hugo P Nov 20 '14 at 02:05
  • So something isn't working when the create statement line is happening. Need to print out the stack trace, and then go from there. – CBredlow Nov 20 '14 at 02:10
  • 1
    *When you get an exception it is completely and utterly pointless to print anything that doesn't include the message from the exception.* ***Lose this habit.*** Change your `catch` block to print the entire stack trace and post it here, in your question. – user207421 Nov 20 '14 at 02:14
  • 1
    As expected, the text of the exception now answers your question completely. There was no need for any of this. NB Netbeans has zero relevance to this question. – user207421 Nov 20 '14 at 04:47

1 Answers1

1

You can't manipulate tables (insert, update) with the executeQuery() method. You'll need to run stmt.executeUpdate(request);.

https://docs.oracle.com/javase/7/docs/api/java/sql/Statement.html#executeUpdate%28java.lang.String%29

CBredlow
  • 2,790
  • 2
  • 28
  • 47