ورود

View Full Version : مبتدی: اضافه کردن رکورد جدید به ResultSet



csharp.net
دوشنبه 12 مهر 1395, 17:06 عصر
سلام دوستان
من میخوام یه رکورد به ResultSet اضافه کنم ، کدم رو هم به صورت زیر نوشتم ولی پیغام ResultSet non-updatable میده، ممنون میشم راهنمایی بفرمایید که مشکل از کجاست



try
{
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
// Class.forName("com.mysql.jdbc.Driver").newInstance();
String url= "jdbc:mysql://localhost/university?user=root&password=";
Connection connection = DriverManager.getConnection(url);
//connection.setReadOnly(false);
Statement statement1 = connection.createStatement(ResultSet.TYPE_SCROLL_S ENSITIVE,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.moveToCurrentRow();

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

PeaceKeeper
پنج شنبه 15 مهر 1395, 23:09 عصر
ابتدا از وجود primery key در جدول مطمئن شوید و سپس به جای این دستور select از "SELECT * FROM students FOR UPDATE" استفاده کنید. در کل کلید اصلی باید در دستور select قید شود.