ResultSet不可更新

时间:2016-10-03 19:59:29

标签: java mysql resultset

我想在ResultSet中添加一行。我的代码工作正常然后停止说结果集不可更新。我在下面写了测试代码来演示问题。

try 
{
    DriverManager.registerDriver(new com.mysql.jdbc.Driver());
    String url= "jdbc:mysql://localhost/university?user=root&password=";
    Connection connection = DriverManager.getConnection(url);
    Statement statement1 = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
    ResultSet result = statement1.executeQuery("SELECT FirstName FROM Students");

    if (result.getConcurrency() == ResultSet.CONCUR_READ_ONLY) 
    {
       System.out.println("ResultSet non-updatable.");
    } 
    else 
    {
       System.out.println("ResultSet updatable.");
    }

    result.moveToInsertRow(); 
    result.updateString("FirstName", "Alex");
    result.insertRow();


    result.beforeFirst();

    while (result.next()) 
    {
        System.out.println(result.getString("FirstName"));

    }

    result.close();
    statement1.close();
    connection.close();
} 
catch (Exception ex) 
{
    System.out.println(ex.getMessage());
}

0 个答案:

没有答案