2015-11-08 107 views
0

我正在爲我的學校項目使用mysql和Netbeans java。每當我嘗試註冊的細節到SQL,我得到這個錯誤My SqlSyntaxErrorException:Unknown Column " [the data in the text field] in 'field list'" 這裏是我的代碼:連接mysql和netbeans數據時出錯

int age = Integer.parseInt(AgeTF.getText()); 
    String name=NameTF.getText(); 
    String id=IDTF.getText(); 
    String dob=DobTF.getText(); 
    String address=AddressTF.getText(); 

    try {   
     Class.forName("java.sql.Driver"); 
     String database = "jdbc:mysql://localhost:3306/final"; 
     Connection conn = DriverManager.getConnection(database, "root", "sanchit"); 
     Statement stmt = conn.createStatement(); 
     String sql = "insert into aadhar values ('" + id + "', " + name + ", '" + dob + "' , '" + age + "' , '" + address + "');" ; 
     stmt.executeUpdate(sql); 
     } 
    catch(Exception e){ 
     JOptionPane.showMessageDialog(null,"" + e); 
    } 
    JOptionPane.showMessageDialog(this,"You have been registered!"); 

請幫助。 感謝

回答

0

你沒有保護簡單報價:

寫的'" + name + "'代替" + name + "

String sql = "insert into aadhar values ('" + id + "', '" + name + "', '" + dob + "' , '" + age + "' , '" + address + "');" ; 
+0

謝謝!傻我:/ – sam007

+0

不客氣!我們都犯這樣的錯誤,不要擔心...... –