简体   繁体   English

当我尝试o插入数据时dataexecuteQuery()无法正常工作吗?

[英]When I try o insert data dataexecuteQuery() isn't working?

Can't seem to fix this. 似乎无法解决此问题。 Been trying to get it to work for the past hour. 一直在努力使其在过去一个小时内正常工作。 any help would be appreciated. 任何帮助,将不胜感激。

INFO: Server startup in 868 ms
java.sql.SQLException: Can not issue data manipulation statements with executeQuery().Event{id=0, name='dads', venue='dasd', startDate='11/11/11', endDate='12/11/11'}

Seemed to be getting an error when I try to do an insert. 尝试执行插入操作时似乎收到了错误。

public void addEvent(Event event) throws DaoException{
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            con = this.getConnection();

            String query = "INSERT INTO TABLE EVENT VALUES(null, ?, ?, ?, ?)";         
            ps = con.prepareStatement(query);
            ps.setString(1, event.getName());
            ps.setString(2, event.getVenue());
            ps.setString(3, event.getStartDate());
            ps.setString(4, event.getEndDate());

            rs = ps.executeQuery();
        }catch(SQLException e) {
            System.out.println(event.toString());
            e.printStackTrace();
        }finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (ps != null) {
                    ps.close();
                }
                if (con != null) {
                    freeConnection(con);
                }
            } catch (SQLException e) {
                throw new DaoException("Couldn't " + e.getMessage());
            }
        }
    }

for inserting or updating or deleting you should use executeUpdate () 对于插入,更新或删除,应使用executeUpdate ()

executeUpdate() returns int value executeUpdate()返回int

so replace this line rs = ps.executeQuery(); 因此,请替换此行rs = ps.executeQuery(); with

int result = ps.executeUpdate();

Note you will get another error after modifying as per above because you sql query is also wrong 请注意 ,按照上述进行修改后,您将收到另一个错误,因为您的sql查询也是错误的

Use the following query 使用以下查询

INSERT INTO EVENT VALUES(null, ?, ?, ?, ?)

it looks like incorrect syntax of INSERT Query, update it likewise 它看起来像INSERT Query的语法不正确,请同样进行更新

INSERT INTO EVENT VALUES(null, ?, ?, ?, ?) // remove TABLE word
//INSERT INTO TABLE_NAME VALUES(...) , this is correct syntax.

also correct here too 在这里也正确

    executeUpdate() instead of executeQuery()
// for insert/update/delete query always use executeUpdate(),
// for select query use executeQuery()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM