Java准备语句不使用自动增量

时间:2016-06-14 12:03:49

标签: java mysql prepared-statement

我试图创建一个小的测试java restful web服务来将记录插入数据库。 我的表是:

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();
        } 
    }
}

3 个答案:

答案 0 :(得分:0)

你不应该为id赋值(由db引擎自动管理)

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

答案 1 :(得分:0)

尝试使用stmt.execute 当我使用executeQuery时,我得到了Exception

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 

答案 2 :(得分:0)

它解决了。我犯了一个错误。我必须在数据库中设置默认值。