public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver"); // 加载数据库驱动
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test", "root", "111");
// 与数据库建立连接
Statement statement = conn.createStatement();
// 获取Statement对象
statement.execute("insert into person values(null,'小芳','女','1987-05-01')");
conn.close();
// 执行添加数据操作
System.out.println("添加数据的行数为:" + statement.getUpdateCount());
} catch (Exception e) {
e.printStackTrace();
} // 异常处理
}

解决方案 »

  1.   

    把insert into person values(null,'小芳','女','1987-05-01')拷贝到数据库里直接执行下看看。应该是你的SQL语句有问题。
      

  2.   

    int getUpdateCount()
                       throws SQLException以更新计数的形式检索当前结果;如果结果为 ResultSet 对象或没有更多结果,则返回 -1。每个结果只应调用一次此方法。
      

  3.   

    我觉得很无语
    你Connection对象都已经关闭了,还去getUpdateCount有啥意义
    try {
        statement.getUpdateCount();
    } catch(SQLException e) {} finally {
        if (statement != null) {
            try {
                statement.close();
            } catch(SQLException e) {}
        }
        if (conn != null) {
            try {
                conn.close();
            } catch(SQLException e) {}
        }
    }
    正确的执行顺序应该是这样的。实在不行,用executeUpdate的返回值一样能够取到更新件数
      

  4.   

    “你Connection对象都已经关闭了,还去getUpdateCount有啥意义”这才是关键问题的!