2014-10-09 87 views
0

我想用jdbc將一些數據插入到我的數據庫中。我正在使用正確的表格,數據庫名稱和參數。一切都被檢查。JDBC插入錯誤

代碼

public static void main(String[] args) throws IOException/*, ClassNotFoundException, SQLException*/ { 
    DataInputStream d = new DataInputStream(System.in); 
    System.out.println("Enter employee ID : "); 

    @SuppressWarnings("deprecation") 
    int empid = Integer.parseInt(d.readLine()); 

    System.out.println("Enter employee name : "); 

    @SuppressWarnings("deprecation") 
    String empname = d.readLine(); 

    System.out.println("Enter employee ID : "); 
    @SuppressWarnings("deprecation") 
    double empsalary = Double.parseDouble(d.readLine()); 

    try { 
     Class.forName("com.mysql.jdbc.Driver"); 

    } catch (ClassNotFoundException e) { 
     e.printStackTrace(); 
    } 

    Connection con; 
    try { 
     con = DriverManager.getConnection("jdbc:mysql://localhost:3306/testDB","root",""); 
     PreparedStatement pst = con.prepareStatement("insert into `employee` values(?,?,?"); 
     pst.setInt(1, empid); 
     pst.setString(2, empname); 
     pst.setDouble(3, empsalary); 
     int rowcount = pst.executeUpdate(); 
     System.out.println(rowcount + " row has been instered"); 
    } catch (SQLException e) { 

     e.printStackTrace(); 
    } 

這裏是錯誤的,它給我,當我嘗試運行它:

com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: 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 '' at line 1 
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936) 
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985) 
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631) 
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723) 
at com.mysql.jdbc.Connection.execSQL(Connection.java:3283) 
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1332) 
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1604) 
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1519) 
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1504) 
at com.test.jdbcExample.main(jdbcExample.java:47) 

回答

1

你在這一行中有語法錯誤:

PreparedStatement pst = con.prepareStatement("insert into `employee` values(?,?,?"); 

你缺少的價值觀條款右括號。

+0

謝謝你現在工作完美:) – Seekerakos 2014-10-09 18:02:09

2

有值子句中缺少一個右括號。

PreparedStatement pst = con.prepareStatement("insert into employee values(?,?,?)"); 
+0

我從mysql論壇上讀到的,沒有引用我有同樣的問題 – Seekerakos 2014-10-09 17:57:36

+0

@Seekerakos還有缺少的右括號。 – manouti 2014-10-09 18:00:30

+1

@Seekerakos主要問題是缺少關閉圓括號,但通常還指定要插入的列。如果添加了列或者表中列的順序發生更改,那麼當前插入將會中斷。 – 2014-10-09 18:01:45