2013-05-06 22 views
0

我寫了這個代碼中插入MySQL中值的值我的SQL插入,我已經取得了數據庫連接 我收到此錯誤:com.mysql.jdbc.exceptions.MySQLSyntaxErrorException使用Java程序

public class JavaMysql { 

    public static void main(String[] args) { 
     String url = "jdbc:mysql://localhost:3306/bhuwan"; 
     String driver = "com.mysql.jdbc.Driver"; 
     String userName = "root"; 
     String password = "rpass"; 
     try { 
      Class.forName(driver).newInstance(); 

      Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bhuwan","root","rpass"); 
      PreparedStatement stmt=conn.prepareStatement("insert in to xmltable values(?,?)"); 
      stmt.setInt(1,101); 
      stmt.setString(2,"Nitish Sharma"); 
      stmt.execute(); 
      int i=stmt.executeUpdate(); 
      System.out.println(i+"records inserted"); 
      conn.close(); 
     }catch(Exception e){System.out.println(e);} 
     } 
} 
+1

'成''而不是'成'' – Maroun 2013-05-06 06:54:19

回答

4

的問題是你有一個空格在INTO關鍵字,

insert in to xmltable values(?,?) 
     ^causes the error 

應該

insert into xmltable values(?,?) 
+0

現在我得到一個錯誤:-com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException:重複條目'101'爲'PRIMARY'鍵' – 2013-05-06 07:05:53

+0

它只是意味着已經有一個值主鍵存在。你有沒有自動增量欄? – 2013-05-06 07:38:06

+0

是的,我有自動增量列ID – 2013-05-06 08:14:37