Java Insert Query with TextFields error
I am creating a GUI. Which can insert data when the user clicks JButton b1
. The base database has 1 table which is a movie and has two columns. The name of the 1st column title
, and the data type varchar
, and the second column name year
, which has the data type integer
. don't know how to write this query in the program using text boxes. I tried many codes as requested but nothing happened.
Code:
public class A extends JFrame{
private JTextField t1;
private JTextField t2;
private JLabel l1;
private JLabel l2;
private JButton b1;
private Connection conn;
private Statement state;
String server = "jdbc:mysql://localhost/demo";
String user="root";
String pass="65";
public A(){
super("Query");
setLayout(null);
t1= new JTextField();
t1.setBounds(173, 152, 128, 27);
add(t1);
//for year
t2= new JTextField();
t2.setBounds(173, 105, 128, 27);
add(t2);
l1= new JLabel("title");
l1.setBounds(99, 101, 42, 37);
add(l1);
l1= new JLabel("year");
l1.setBounds(99, 148, 42, 37);
add(l1);
b1= new JButton("Insert");
b1.setBounds(50, 222, 102, 37);
add(b1);
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
CreateQuery("insert into movies (title,year) values('"+t1.getText()+"') ");
}
catch(Exception e1){
e1.printStackTrace();
}
}
});
setSize(450,450);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
}
public void CreateQuery(String w){
try{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection(server,user,pass);
state = conn.createStatement();
state.executeUpdate(w);
JOptionPane.showMessageDialog(null, "Query Executed");
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e.getMessage());
}
}
}
Main:
public class Main {
public static void main(String[] args) {
A obj = new A();
}
}
You can do this in two ways: the first is the procedure you are using, i.e. using createStatement
In your case, you need to set the title and year.
CreateQuery("insert into movies (title,year) values('"+t1.getText()+"', " + t2.getText() + ")");
And the second is using prepareStatement, Method call,
CreateQuery(t1.getText(), t2.getText());
Method,
public void CreateQuery(String title, String year){
try{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection(server,user,pass);
PreparedStatement state = conn.prepareStatement("insert into movies (title,year) values('?', ?)");
state.setString(1, title);
state.setInt(1, new Integer(year));
state.executeUpdate();
JOptionPane.showMessageDialog(null, "Query Executed");
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e.getMessage());
}
}
source to share
You have insert into movies (title,year) values('"+t1.getText()+"')
you need two parameters and you only send one.
--- Edit ---
Replace Statement
with PreparedStatement
and use parameters with methods setInt
and setString
. Good practice check the following link http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html
source to share
By sending two arguments which are the title and the year can solve the problem
Modified code
CreateQuery("insert into movies (title,year) values('"+t1.getText()+"',"+t2.getText()+") ");
if you have a default value for the year field, below is the modified code.
CreateQuery("insert into movies (title) values('"+t1.getText()+"') ");
source to share
b1= new JButton("Insert");
b1.setBounds(50, 222, 102, 37);
add(b1);
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
int w = Integer.parseInt(t2.getText());
CreateQuery("insert into movies (title,year) values('"+t1.getText()+"',"+w+") ");
}
catch(Exception e1){
e1.printStackTrace();
}
Exception error on click
java.lang.NumberFormatException: For input string: "World"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at Queryahmad.C_03Update$1.actionPerformed(C_03Update.java:76)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)