Results 1 to 17 of 17
  1. #1
    Sociopath is offline Newbies
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Default Using jbutton to write to jtextfield values to database

    Hi, i'm trying to write a GUI which can write text from a JTextfield to a MySQL database when the user clicks a jbutton. I'm having errors with the eventhandler code for the button, can anyone take a look at my code for me and explain to me what i'm doing wrong:

    Java Code:
    import java.awt.*;
    import java.sql.*;
    import javax.swing.*;
    
    
    class  ContractYearly{
    	
    
    
    
        public static void main(String[] args){
        	
        JFrame f=new JFrame();
        JLabel label1=new JLabel("Name: ");
        JLabel label2=new JLabel("Address: ");
        JLabel label3=new JLabel("");
        JButton SubmitButton=new JButton("Submit");
        JTextField text1=new JTextField(20);
        JTextField text2=new JTextField(20);
        try{
        	
               Class.forName("com.mysql.jdbc.Driver");
               Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/MyStudentProfile", "root", "hejsan123");
               
               Statement st=con.createStatement();
               ResultSet rs=st.executeQuery("INSERT INTO contractyearly" +
               		"VALUES (12, ");
               String name="",address="";
               if(rs.next()){
                   name=rs.getString("name");
                   address=rs.getString("address");
               }
               text1.setText(name);
               text2.setText(address);
              }
            catch(Exception e){
            }
            JPanel p=new JPanel(new GridLayout(2,2));
            p.add(label1);
            p.add(text1);
            p.add(label3);
            p.add(label2);
            p.add(text2);
            p.add(SubmitButton);
            f.add(p);
            f.setVisible(true);
            f.pack();
        }
            private void SubmitButtonActionPerformed(java.awt.event.ActionEvent evt) {
    
                String s1 = text1.getText();
                String s2 = text2.getText();
    
         try{
                Statement stmt = this.con.createStatement();
         stmt.executeUpdate("insert into employee (First_Name ,Last_Name , Address , Salary ) values (" + s1+ ","+s2+ ")");
    
         }catch(Exception e){
    
         System.out.println(e.toString());
          
         }
         }
    
    
    }
    Last edited by Fubarable; 05-24-2011 at 02:57 AM. Reason: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    It would help immensely if you would post the actual error messages themselves and indicate in the code above which lines are causing the errors.

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    6

    Default

    I'm having errors with the eventhandler code for the button,
    Well, I'm not a mind reader and I can't execute your code since I don't have access to your database and I don't know what your errors are so I can't really help.

    My only suggestion is to use a PreparedStatement. If will simplify the SQL logic so you are less likely to have SQL problems. The basics would be:

    Java Code:
    String sql = "INSERT INTO Page (Name, Title) VALUES (?, ?)";
    PreparedStatement stmt = connection.prepareStatement(sql);
    stmt.setString( 1, "Name1" );
    stmt.setString( 2, "Title1" );
    stmt.executeUpdate();

  4. #4
    Sociopath is offline Newbies
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Default

    There's no problem with my SQL, I just need to tidy it up. My problem is with the code for the jbutton, i'm getting the following errors:
    con cannot be resolved or is not a field
    text1 cannot be resolved
    text2 cannot be resolved

    My plan is to insert the values into my database once the button is pressed.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

  6. #6
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

  7. #7
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    Quote Originally Posted by Sociopath View Post
    Hi, i'm trying to write a GUI which can write text from a JTextfield to a MySQL database when the user clicks a jbutton. I'm having errors with the eventhandler code for the button, can anyone take a look at my code for me and explain to me what i'm doing wrong:

    Java Code:
    import java.awt.*;
    import java.sql.*;
    import javax.swing.*;
    
    
    class  ContractYearly{
    
    
    
    
        public static void main(String[] args){
    
        JFrame f=new JFrame();
        JLabel label1=new JLabel("Name: ");
        JLabel label2=new JLabel("Address: ");
        JLabel label3=new JLabel("");
        JButton SubmitButton=new JButton("Submit");
        JTextField text1=new JTextField(20);
        JTextField text2=new JTextField(20);
        try{
    
               Class.forName("com.mysql.jdbc.Driver");
               Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/MyStudentProfile", "root", "hejsan123");
    
               Statement st=con.createStatement();
               [b]
               ResultSet rs=st.executeQuery("INSERT INTO contractyearly" +
               		"VALUES (12, ");[/b]
               String name="",address="";
               if(rs.next()){
                   name=rs.getString("name");
                   address=rs.getString("address");
               }
               text1.setText(name);
               text2.setText(address);
              }
            catch(Exception e){
            }
            JPanel p=new JPanel(new GridLayout(2,2));
            p.add(label1);
            p.add(text1);
            p.add(label3);
            p.add(label2);
            p.add(text2);
            p.add(SubmitButton);
            f.add(p);
            f.setVisible(true);
            f.pack();
        }
            private void SubmitButtonActionPerformed(java.awt.event.ActionEvent evt) {
    
                String s1 = text1.getText();
                String s2 = text2.getText();
    
         try{
                Statement stmt = this.con.createStatement();
         stmt.executeUpdate("insert into employee (First_Name ,Last_Name , Address , Salary ) values (" + s1+ ","+s2+ ")");
    
         }catch(Exception e){
    
         System.out.println(e.toString());
    
         }
         }
    
    
    }
    The only thing that I can see is your INSERT INTO query, if not continued to next line, will look like this
    Java Code:
    ResultSet rs=st.executeQuery("INSERT INTO contractyearlyVALUES (12, ");
    Which should be
    Java Code:
    ResultSet rs=st.executeQuery("INSERT INTO contractyearly VALUES (12, ");

  8. #8
    Sociopath is offline Newbies
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Default

    Does this really matter??? I'm only trying to find a solution to my problem.

  9. #9
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    I am just showing your error, but camickr is correct, it is better to use PreparedStatement than concatenated strings
    Last edited by mine0926; 05-24-2011 at 04:04 AM.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Sociopath View Post
    Does this really matter??? I'm only trying to find a solution to my problem.
    It doesn't matter if you were paying us to help you, as we'd get paid whether you cross-posted or not, but you're not, and nor should you. Instead you're asking volunteers to help you on their own time and out of the goodness of their heart, and it can bother us if we spend a lot of time and effort helping only to find that that effort was wasted as the question had already been answered in a cross-post. We don't say that it's wrong to cross-post, but we do ask that if you respect us and appreciate our help, that you tell us if and when you cross-post and provide links. That's really not asking too much, is it?
    Last edited by Fubarable; 05-24-2011 at 04:14 AM.

  11. #11
    Sociopath is offline Newbies
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    It doesn't matter if you were paying us to help you, as we'd get paid whether you cross-posted or not, but you're not, and nor should you. Instead you're asking volunteers to help you on their own time and out of the goodness of their heart, and it can bother us if we spend a lot of time and effort helping only to find that that effort was wasted as the question had already been answered in a cross-post. We don't say that it's wrong to cross-post, but we do ask that if you respect us and appreciate our help, that you tell us if and when you cross-post and provide links. That's really not asking too much, is it?
    I will edit my post if my problem is solved elsewhere. I already know this issue, but sometimes a problem needs many perspectives.

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Sociopath View Post
    I will edit my post if my problem is solved elsewhere. I already know this issue, but sometimes a problem needs many perspectives.
    Again, we aren't against your cross-posting, but again all we ask as that you notify us and provide links, and again, this isn't asking too much. If you decline to do this, there are many here who would think twice about helping you.

  13. #13
    Sociopath is offline Newbies
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Default

    Java Code:
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.sql.*;
    
    import javax.swing.*;
    
    public class ContractYear extends JFrame {
    	
    	JFrame f=new JFrame();
        JLabel label1=new JLabel("Name: ");
        JLabel label2=new JLabel("Address: ");
        JLabel label3=new JLabel("");
        JButton SubmitButton=new JButton("Submit");
        JTextField text1=new JTextField(20);
        JTextField text2=new JTextField(20);
        JPanel p=new JPanel(new GridLayout(2,2));
        {
        p.add(label1);
        p.add(text1);
        p.add(label3);
        p.add(label2);
        p.add(text2);
        p.add(SubmitButton);
        f.add(p);
        f.setVisible(true);
        f.pack();
        }
    	public static void main(String[] args) throws SQLException {
    
    	}
    	private void SubmitButtonActionPerformed(java.awt.event.ActionEvent evt) {
    		
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/MyStudentProfile", "root", "hejsan123");
    
            try{
            Statement st=con.createStatement();
            
            String s1=text1.getText();
            String s2=text2.getText();
    		st.executeUpdate("insert into employee (Forename , Sirname ) values (" + s1+ ","+s2+ ")");
            
            }catch(Exception e){
            System.out.println(e.toString());
    		
            }
    	}
    }
    Now getting an error with:
    Java Code:
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/MyStudentProfile", "root", "hejsan123");
    Unhandled exception type ClassNotFoundException

    Any ideas?

  14. #14
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    Quote Originally Posted by Sociopath View Post
    Does this really matter??? I'm only trying to find a solution to my problem.
    I gave you a link on the other forum that explains all about it, but you saw fit to ask the question and cause members here to answer it -- thus creating a prime example of the evils of cross posting.

    db

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Sociopath View Post
    Now getting an error with:
    Java Code:
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/MyStudentProfile", "root", "hejsan123");
    Unhandled exception type ClassNotFoundException

    Any ideas?
    Look at the API for Class.forName(...) and you'll see that it throws this exception, and just as with any checked exception you must deal with it when calling the method, usually by either having the method this is called in throw the same exception if you want it handled up stream, or placing it in a try/catch block if you're going to handle it in that method. The Java tutorials on Exceptions covers all of this if you haven't studied it yet.

  16. #16
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    6

    Default

    I already know this issue, but sometimes a problem needs many perspectives.
    No it doesn't usually need many perspectives. What it needs is a proper question will all the necessary information needed to solve the problem.

    If you had asked a proper question with the proper error information and a proper Short, Self Contained, Correct Example then this question would have been answered long ago. If the SQL isn't the issue then why did you post that code? If you want better answers then post a better SSCCE.

  17. #17
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by camickr View Post
    If the SQL isn't the issue then why did you post that code?
    The funny thing is, even if the SQL, in this moment, is not the problem it is only because some other problem is occurring first. It will be a problem, though, as, in the OP, he is attempting to insert text without quoting the fields and, hopefully, two of the fields are either nullable and/or have default values, and only two fields.

    That entire thing is just a mess, anyway. Mixing of GUI, business, and model code, all the actions taking place on the event thread, and very poor sql-related code.

Similar Threads

  1. Replies: 7
    Last Post: 08-29-2010, 10:33 AM
  2. Write-only JTextField?
    By ProspectiveDeveloper in forum AWT / Swing
    Replies: 5
    Last Post: 05-11-2010, 11:13 AM
  3. Highlight JTextField using Jbutton?
    By goffy in forum New To Java
    Replies: 11
    Last Post: 05-02-2010, 11:41 AM
  4. Adding jTextField() content to the database?
    By Stephen Douglas in forum New To Java
    Replies: 12
    Last Post: 04-08-2010, 09:50 AM
  5. Replies: 3
    Last Post: 01-07-2010, 12:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •