Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: java + mysql update

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    27
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default java + mysql update

    I am having trouble with my program regarding execute update statement

    this code is working
    val = st.executeUpdate("INSERT test VALUES("+3+","+"'Test1'"+","+"'Test2'"+");");

    but not this one
    int val = st.executeUpdate("insert test values ("+3+","+txtFName.getText()+","+txtLName.getText()+");");


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,225
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: java + mysql update

    1) Sometimes when debugging things such as this, it helps to place the full query into a string and use a println statement to see what the query actually contains.
    2) you mention it one works and the other does not, define 'does not'...is there an exception thrown?
    3) What are txtFName and txtLName - what are their values?
    4) Recommend you use a PreparedStatement

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    27
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: java + mysql update

    here is my action performed
    public void actionPerformed (ActionEvent event)
    		{	
    		//String dbtime;
                    Connection con = null;
                    String url = "jdbc:mysql://localhost:3306/";
                    String db = "WaterStation";
                    String driver = "com.mysql.jdbc.Driver";
                    String FName = txtFName.getText();
                    String LName = txtLName.getText();
                    try{
                        Class.forName(driver);
                        con = DriverManager.getConnection(url+db,"root",[password]);
                        try{
                            Statement st = con.createStatement();
                            int val = st.executeUpdate("insert test values ("+3+","+txtFName.getText()+","+txtLName.getText()+");");
                           JOptionPane.showMessageDialog(null,txtFName.getText()+" "+txtLName.getText()+" is added");
                         }
                        catch (Exception sq){
                            JOptionPane.showMessageDialog(null, "SQL statement is not executed");
                        }
                    }
                    catch (Exception e){
                        e.printStackTrace();
                    }
                    } //end action performed
            }  //end class

    when I use .getText() the catch Eception sq occurs

  4. #4
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,225
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: java + mysql update

    This does not directly address my points...what is the exception? Post the full stack trace.

  5. The Following User Says Thank You to copeg For This Useful Post:

    A4Andy (August 26th, 2011)

  6. #5
    Junior Member
    Join Date
    Aug 2011
    Posts
    27
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: java + mysql update

    I added sq.printStackTrace() in the catch sq

    here is my sq.printStackTrace()
    com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Unknown column 'first' in 'field list'
    	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
    	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
    	at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)
    	at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
    	at com.mysql.jdbc.Connection.execSQL(Connection.java:3277)
    	at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1402)
    	at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1317)
    	at GUI$HandlerClass1.actionPerformed(GUI.java:115)
    	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2012)
    	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2335)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:404)
    	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:253)
    	at java.awt.Component.processMouseEvent(Component.java:6203)
    	at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
    	at java.awt.Component.processEvent(Component.java:5968)
    	at java.awt.Container.processEvent(Container.java:2105)
    	at java.awt.Component.dispatchEventImpl(Component.java:4564)
    	at java.awt.Container.dispatchEventImpl(Container.java:2163)
    	at java.awt.Component.dispatchEvent(Component.java:4390)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4461)
    	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4125)
    	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4055)
    	at java.awt.Container.dispatchEventImpl(Container.java:2149)
    	at java.awt.Window.dispatchEventImpl(Window.java:2478)
    	at java.awt.Component.dispatchEvent(Component.java:4390)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:649)
    	at java.awt.EventQueue.access$000(EventQueue.java:96)
    	at java.awt.EventQueue$1.run(EventQueue.java:608)
    	at java.awt.EventQueue$1.run(EventQueue.java:606)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:116)
    	at java.awt.EventQueue$2.run(EventQueue.java:622)
    	at java.awt.EventQueue$2.run(EventQueue.java:620)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:619)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)

    the 'first' is the one that i wrote in JTextField txtFName
    Last edited by A4Andy; August 26th, 2011 at 05:11 AM.

  7. #6
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,225
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: java + mysql update

    You have an error in your syntax. Please read my first post. To reiterate: using System.out.println before calling the query that works, and compare that to the query that doesn't work. You will see there are missing quotes - which would be readily avoided if you used a PreparedStatement as I suggested

  8. #7
    Junior Member
    Join Date
    Aug 2011
    Posts
    27
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: java + mysql update

    I switched to prepared statement and it is working fine now

Similar Threads

  1. [SOLVED] HELP ME! How to update quantity (JAVA)
    By kerrina in forum What's Wrong With My Code?
    Replies: 12
    Last Post: August 2nd, 2011, 03:27 AM
  2. Problem with Java/MySQL query...
    By RiskyShenanigan in forum JDBC & Databases
    Replies: 2
    Last Post: March 28th, 2011, 03:50 AM
  3. Java.jar and Mysql.jar
    By kurt-hardy in forum Java Theory & Questions
    Replies: 1
    Last Post: January 27th, 2011, 08:48 AM
  4. Java Applet and MySQL
    By Terillius in forum JDBC & Databases
    Replies: 4
    Last Post: August 21st, 2010, 10:05 PM
  5. Use of Unicode 5.1 in MySQL DB with Java
    By Desert Fox in forum JDBC & Databases
    Replies: 2
    Last Post: November 12th, 2008, 07:29 AM