Please find the error in this code , I am trying to save the data but it’s not storing
using Eclipse , MySQL , 64 bit
Error :
java.sql.SQLException: [MySQL][ODBC 5.2(w) Driver][mysqld-5.5.28]You
have an error in your SQL syntax; check the manual that corresponds to
your MySQL server version for the right syntax to use near ‘database
(‘did’,’dname’,’dadress’,’salary’,’sex’,’specialist’,’cell#’)VALUES
(?,’ at line 1
package pk.pucit;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
class DoctorsRecord {
public JFrame frm=new JFrame("Doctor's Record Information");
public JButton btn1,btn2,btn3,btn4;
public JLabel lbl1,lbl2,lbl3,lbl4,lbl5,lbl6,lblr;
public JTextField txt1,txt2,txt3,txt4,txt5,txt6,txt7;
public JRadioButton rbtn1,rbtn2;
//frm.setBackground(Color.green);
public DoctorsRecord(){
lbl1=new JLabel("Doctor ID");
lbl2=new JLabel("Doctor's Name");
lbl3=new JLabel("Address");
lblr=new JLabel("Gender");
lbl4=new JLabel("Salary");
lbl5=new JLabel("Rank");
lbl6=new JLabel("Ph #");
txt1=new JTextField(7);
txt2=new JTextField(7);
txt3=new JTextField(7);
txt4=new JTextField(7);
txt5=new JTextField(7);
txt6=new JTextField(7);
txt7=new JTextField(7);
/*rbtn1=new JRadioButton("Male");
rbtn2=new JRadioButton("Female");*/
btn1=new JButton("Save");
btn2=new JButton("Search ");
btn3=new JButton("Reset");
btn4=new JButton("Cancel");
JPanel pnl1=new JPanel();
pnl1.add(lbl1);
pnl1.add(txt1);
pnl1.add(lbl2);
pnl1.add(txt2);
pnl1.add(lbl3);
pnl1.add(txt3);
pnl1.add(lblr);
pnl1.add(txt7);
pnl1.add(lbl4);
pnl1.add(txt4);
pnl1.add(lbl5);
pnl1.add(txt5);
pnl1.add(lbl6);
pnl1.add(txt6);
pnl1.setLayout(new GridLayout(8,1));
JPanel pnl=new JPanel();
pnl.add(btn1);
pnl.add(btn2);
pnl.add(btn3);
pnl.add(btn4);
pnl.setLayout(new GridLayout(1,2));
frm.add(pnl1,BorderLayout.CENTER);
frm.add(pnl,BorderLayout.SOUTH);
//frm.setLayout(new GridLayout(10,1));
frm.setBounds(400,100,400,500);
frm.setVisible(true);
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
btn1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae){
try{
String id=txt1.getText();
String name=txt2.getText();
String add=txt3.getText();
String sal=txt4.getText();
String sex=txt5.getText();
String sep=txt6.getText();
String cell=txt7.getText();
dataHandler(id,name,add,sal,sex,sep,cell);
}
catch(Exception e){
System.out.println("Exception occur");
}
}
}
);
}
public void dataHandler(String id,String dn,String add,String sal,String sex,String sep,String cell)throws ClassNotFoundException, SQLException{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:Dbms", "root", "root");
String query = "insert into database ('did','dname','dadress','salary','sex','specialist','cell#')" +"VALUES (?,?,?,?,?,?,?)";
PreparedStatement preparedStatement = con.prepareStatement(query);
preparedStatement.setString(1, id);
preparedStatement.setString(2, dn);
preparedStatement.setString(3, add);
preparedStatement.setString(4, sal);
preparedStatement.setString(5, sex);
preparedStatement.setString(6, sep);
preparedStatement.setString(7, cell);
preparedStatement.execute();
con.close();
}catch(Exception oc){
System.out.println(oc.toString());
}
}
public static void main(String []args){
new DoctorsRecord();
}
}
There are two problems with you query.
First,
DataBaseis a Reserved Keyword, you must escape it with backtick.Second, the column names must not be wrap with single quotes. because if you do that, it will also generate an error telling you that the column was not found. Only
columnNamesandtableNamescan be enclosed with backtick.