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:
Code java:
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
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
Re: Using jbutton to write to jtextfield values to database
Quote:
Originally Posted by
copeg
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
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)
Re: Using jbutton to write to jtextfield values to database
Re: Using jbutton to write to jtextfield values to database
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:
Code :
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/MyStudentProfile", "root", "hejsan123");
Unhandled exception type ClassNotFoundException
Any ideas?