2016-06-14 25 views
-2

我想創建一個小測試java restful web服務插入記錄到數據庫中。 我的表是:Java準備語句不能與自動增量

test (id int(5) PRIMARY KEY AUTO_INCREMENT, name varchar(10)) 

當我嘗試以下方法(與ID改爲每次),它的工作原理:

PreparedStatement stmt; 
stmt = con.prepareStatement("insert into test (id,name) values (?,?)"); 
stmt.setInt(1, 1); 
stmt.setString(2, "test"); 
stmt.executeUpdate(); 

但是,當我試圖做到這一點,它不工作:

PreparedStatement stmt; 
stmt = con.prepareStatement("insert into test (name) values (?)"); 
stmt.setString(1, "test"); 
stmt.executeUpdate(); 

下面是完整的代碼: Database.java

package dao; 
import java.sql.Connection; 
import java.sql.DriverManager; 
public class Database 
{ 
    public Connection getConnection() throws Exception 
    { 
     try 
     { 
      String connectionURL = "jdbc:mysql://localhost:3306/rit"; 
      Connection connection = null; 
      Class.forName("com.mysql.jdbc.Driver").newInstance(); 
      connection = DriverManager.getConnection(connectionURL, "root", ""); 
      return connection; 
     } 
     catch (Exception e) 
     { 
      throw e; 
     } 
    } 
} 

testService.java

package webService; 
import javax.ws.rs.POST; 
import javax.ws.rs.Path; 
import javax.ws.rs.Produces; 
import javax.ws.rs.FormParam; 
import javax.ws.rs.Consumes; 
import model.AccessManager; 
@Path("/test/") 
public class testService 
{ 
    @POST 
    @Consumes("application/x-www-form-urlencoded") 
    @Produces("application/text") 
    public void setfood(@FormParam("name") String name) 
    { 
     AccessManager am = new AccessManager(); 
     try 
     { 
      am.setFood(name); 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 
    } 
} 
+2

什麼意思不工作?任何例外,你可以告訴我們? – Jens

+0

不,沒有東西會被返回。 –

回答

0

它解決了。我犯了一個錯誤。我不得不在數據庫中設置一個默認值。

0

你不應該assing的id值(自動由數據庫引擎管理)

stmt = con.prepareStatement("insert into test (name) values (?)"); 
stmt.setInt(1, "test"); 
stmt.executeUpdate(); 
+0

是的,這就是我的嘗試。看到我的消息的第二部分。但它不工作。 –

+0

你看過db ...嗎? ..沒有錯誤? 。沒有結果 ? ..似乎很奇怪..你確定你在調用正確的函數嗎? – scaisEdge

0

嘗試使用stmt.execute 當我使用executeQuery我得到異常

Database database = new Database(); 
     try { 
      PreparedStatement ps = database.getConnection().prepareStatement("insert into test (name) values (?)"); 
      ps.setString(1, "test data"); 
      ps.executeQuery(); 

     } catch (SQLException e) { 
      e.printStackTrace(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 


java.sql.SQLException: Can not issue data manipulation statements with executeQuery(). 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:963) 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:896) 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:885) 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860) 
    at com.mysql.jdbc.StatementImpl.checkForDml(StatementImpl.java:463) 
    at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1926) 
    at com.allex.test2.Start.<init>(Start.java:17) 
    at com.allex.test2.Start.main(Start.java:9) 

但是然後我使用ps.execute();一切正常,並在數據庫apear新行:)

Database database = new Database(); 
     try { 
      PreparedStatement ps = database.getConnection().prepareStatement("insert into test (name) values (?)"); 
      ps.setString(1, "test data "); 
      ps.execute(); 

      ResultSet rs = database.getConnection().createStatement().executeQuery("SELECT * FROM test"); 
      while(rs.next()) 
       System.out.println("ID:"+rs.getInt(1)+" Name:"+rs.getString(2)); 

     } catch (SQLException e) { 
      e.printStackTrace(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
ID:1 Name:test data 
ID:2 Name:test data 
ID:3 Name:test data