📄 updatingdemo.java
字号:
package results;
import java.sql.*;
import java.io.*;
import connections.*;
public class UpdatingDemo {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rset = null;
try {
conn = ConnectionFactory.getConnection();
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
String sql = "SELECT recordingid, tracknumber, tracktitle "
+ "FROM tracks ORDER BY recordingid, tracknumber";
rset = stmt.executeQuery(sql);
// if (rset.next()) {
// rset.beforeFirst();
boolean done = false;
BufferedReader inr =
new BufferedReader(new InputStreamReader(System.in));
while (!done) {
System.out.print("\nType F (first), N (next), P (previous), "
+ "L (last), Q (quit), I (insert), U (update) : ");
String s = inr.readLine();
boolean result = false;
if (s.equalsIgnoreCase("F")) {
result = rset.first();
} else if (s.equalsIgnoreCase("N")) {
result = rset.next();
} else if (s.equalsIgnoreCase("P")) {
result = rset.previous();
} else if (s.equalsIgnoreCase("L")) {
result = rset.last();
} else if (s.equalsIgnoreCase("Q")) {
done = true;
} else if (s.equalsIgnoreCase("U")) {
updateRow(rset, inr, false);
} else if (s.equalsIgnoreCase("I")) {
rset.moveToInsertRow();
updateRow(rset, inr, true);
}
if (done) {
break;
}
if (result) {
System.out.println("cursor moved to row " + rset.getRow());
System.out.println("Record Id : " + rset.getInt(1)
+ ", Track Number : " + rset.getInt(2)
+ ", Track Title : " + rset.getString(3));
} else {
if (!s.equalsIgnoreCase("U") &&!s.equalsIgnoreCase("I")) {
System.out.println("Could not move cursor");
}
}
}
inr.close();
// } else {
// System.out.println("ResultSet contained no rows");
// }
} catch (Exception e) {
e.printStackTrace();
}
finally {
ConnectionFactory.close(rset);
ConnectionFactory.close(stmt);
ConnectionFactory.close(conn);
}
}
public static void updateRow(ResultSet rset, BufferedReader inr,
boolean insert) throws SQLException,
IOException {
System.out.print("Enter Recording Id: ");
String recordId = inr.readLine();
rset.updateInt("RECORDINGID", Integer.parseInt(recordId));
System.out.print("Enter Track Number: ");
String trackNumber = inr.readLine();
rset.updateInt("TRACKNUMBER", Integer.parseInt(trackNumber));
System.out.print("Enter Track Title: ");
String trackTitle = inr.readLine();
rset.updateString("TRACKTITLE", trackTitle);
System.out.print("Enter Y to commit the change, "
+ "or any other key to stop the update: ");
String choice = inr.readLine();
if (choice.equalsIgnoreCase("Y")) {
if (insert) {
rset.insertRow();
rset.moveToCurrentRow();
} else {
rset.updateRow();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -