Results 1 to 17 of 17
- 05-24-2011, 02:07 AM #1
Newbies
- Join Date
- May 2011
- Posts
- 5
- Rep Power
- 0
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
-
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.
- 05-24-2011, 03:21 AM #3
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,158
- Rep Power
- 5
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.I'm having errors with the eventhandler code for the button,
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();
- 05-24-2011, 03:28 AM #4
Newbies
- Join Date
- May 2011
- Posts
- 5
- Rep Power
- 0
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.
- 05-24-2011, 03:48 AM #5
- 05-24-2011, 03:54 AM #6
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,619
- Rep Power
- 5
And cross-posted at Using jbutton to write to jtextfield values to database
- 05-24-2011, 03:56 AM #7
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
The only thing that I can see is your INSERT INTO query, if not continued to next line, will look like this
Which should beJava Code:ResultSet rs=st.executeQuery("INSERT INTO contractyearlyVALUES (12, ");
Java Code:ResultSet rs=st.executeQuery("INSERT INTO contractyearly VALUES (12, ");
- 05-24-2011, 03:57 AM #8
Newbies
- Join Date
- May 2011
- Posts
- 5
- Rep Power
- 0
- 05-24-2011, 03:59 AM #9
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
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.
-
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.
- 05-24-2011, 04:24 AM #11
Newbies
- Join Date
- May 2011
- Posts
- 5
- Rep Power
- 0
-
- 05-24-2011, 05:48 AM #13
Newbies
- Join Date
- May 2011
- Posts
- 5
- Rep Power
- 0
Now getting an error with: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()); } } }
Unhandled exception type ClassNotFoundExceptionJava Code:Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/MyStudentProfile", "root", "hejsan123");
Any ideas?
- 05-24-2011, 05:49 AM #14
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
-
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.
- 05-24-2011, 06:35 AM #16
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,158
- Rep Power
- 5
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.I already know this issue, but sometimes a problem needs many perspectives.
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.
- 05-24-2011, 09:29 AM #17
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
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
-
Issue with obtaining data from the JTextField when JButton is pressed
By turc0033 in forum AWT / SwingReplies: 7Last Post: 08-29-2010, 10:33 AM -
Write-only JTextField?
By ProspectiveDeveloper in forum AWT / SwingReplies: 5Last Post: 05-11-2010, 11:13 AM -
Highlight JTextField using Jbutton?
By goffy in forum New To JavaReplies: 11Last Post: 05-02-2010, 11:41 AM -
Adding jTextField() content to the database?
By Stephen Douglas in forum New To JavaReplies: 12Last Post: 04-08-2010, 09:50 AM -
Getting from database, putting into jtextfield
By WLX in forum AWT / SwingReplies: 3Last Post: 01-07-2010, 12:07 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks