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 6 of 6

Thread: Using jbutton to write to jtextfield values to database

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

    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:
    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());
     
         }
         }
     
     
    }
    I'm really sorry that i've just pasted my code, i'm not aware of how to display it in the right format
    Last edited by copeg; May 23rd, 2011 at 07:23 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Using jbutton to write to jtextfield values to database

    First, please do not double post, your other post has been removed. Second, I've edited your post to use the code tags - for future reference please use these tags to make the posted code readable. Third, what is the exact error/exception you are receiving? Lastly, I recommend looking into using a PreparedStatement for what you are trying to do

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

    Sociopath (May 23rd, 2011)

  4. #3
    Junior Member
    Join Date
    May 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Using jbutton to write to jtextfield values to database

    Quote Originally Posted by copeg View Post
    First, please do not double post, your other post has been removed. Second, I've edited your post to use the code tags - for future reference please use these tags to make the posted code readable. Third, what is the exact error/exception you are receiving? Lastly, I recommend looking into using a PreparedStatement for what you are trying to do
    con cannot be resolved or is not a field
    text1 cannot be resolved
    text2 cannot be resolved

  5. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Using jbutton to write to jtextfield values to database

    The variables are out of scope - most are within the main method which, when that method exits, cannot be accessed. Use member variables of a class (a field) and reference them from outside a particular method.
    Suggested reading, which should clarify things a bit: Declaring Member Variables (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

  6. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Using jbutton to write to jtextfield values to database

    This thread has been cross posted here:

    http://www.java-forums.org/database/44445-using-jbutton-write-jtextfield-values-database.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting


  7. #6
    Junior Member
    Join Date
    May 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Using jbutton to write to jtextfield values to database

    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:
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/MyStudentProfile", "root", "hejsan123");

    Unhandled exception type ClassNotFoundException

    Any ideas?

Similar Threads

  1. Jtextfield Validation
    By nimishalex in forum AWT / Java Swing
    Replies: 8
    Last Post: December 11th, 2010, 02:42 AM
  2. JTextField getting text values
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 8
    Last Post: November 18th, 2010, 05:41 PM
  3. [SOLVED] how can i resize JTextField, JButton using GridBagLayout,... TNX
    By sny in forum AWT / Java Swing
    Replies: 0
    Last Post: March 4th, 2010, 09:57 AM
  4. [SOLVED] JTextField not visible in swing
    By Sterzerkmode in forum AWT / Java Swing
    Replies: 4
    Last Post: May 21st, 2009, 07:37 AM