請找到該代碼中的錯誤,我想保存數據,但使用Eclipse,MySQL和64位MySQL數據插入錯誤
錯誤它不是存儲 :
的Java。 sql.SQLException:[MySQL] [ODBC 5.2(w)Driver] [mysqld-5.5.28]您的 在您的SQL語法中有錯誤;檢查對應於您的MySQL服務器版本的手冊,以便在'database' ('did','dname','dadress','salary','sex','specialist','cell#')附近使用正確的語法。 )在直線的數值 (?,」 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();
}
}
StackOverflow的不是這個問題的適當場所。我們不做代碼調試。您需要進行自己的調試,如果您不確定爲什麼某些功能不能按預期工作,請在代碼中加上解釋您期望執行的操作的內容,以及實際執行的操作,包括所有錯誤消息。看[ask advice](http://stackoverflow.com/questions/ask-advice)。 –
你有一個叫做「數據庫」的表嗎? – scones
Thanx兄弟,它現在正在工作, – user1783551