努力解决为什么数据库与访问的链接带来了错误。似乎无法更新数据库中的字段。
/**
* Displaying, inserting into, updating, deleting from a database table.
* This subclass provides a method to update a record with values specified on
* the command line. It inherits all the JDBC connection functionality from
* BorrowerDisplay class.
*
* The updated table is displayed. (Display method inherited from superclass)
*/
import java.sql.*;
import java.util.Scanner;
public class Update extends MainDisplay {
private PreparedStatement sqlUpdate = null;
public boolean connect() {
boolean ok = super.connect(); // superclass's connect () loads the JDBC
try { // driver & connects to the DB engine.
sqlUpdate = connection.prepareStatement(
"UPDATE Ticket SET TicketNo = ?, TransactTime = ?, SaleID = ?, ScheduleID = ?, Price = ? "
+ " WHERE TicketNo = ? AND TransactTime = ? AND SaleID = ? AND ScheduleID = ? AND Price = ?");
} catch (SQLException sqlex) {
System.err.println("SQL Exception");
sqlex.printStackTrace();
ok = false;
}
return ok;
}
public boolean updateTicket() throws SQLException {
Scanner scan = new Scanner(System.in);
System.out.print("TicketNo to change: ");
int TicketNo = scan.nextInt();
System.out.print("Enter current Transaction Time: ");
int TransactTime = scan.nextInt();
System.out.print("Enter new Transaction Time: ");
int newTransactTime = scan.nextInt();
System.out.print("Enter current SaleID: ");
int SaleID = scan.nextInt();
System.out.print("Enter new SaleID: ");
int newSaleID = scan.nextInt();
System.out.print("Enter current ScheduleID: ");
String ScheduleID = scan.next();
System.out.print("Enter new Schedule ID: ");
String newScheduleID = scan.next();
System.out.print("Enter current Price: ");
int Price = scan.nextInt();
System.out.print("Enter new Price: ");
int newPrice = scan.nextInt();
sqlUpdate.setInt(1, TicketNo);
sqlUpdate.setInt(2, newTransactTime);
sqlUpdate.setInt(3, newSaleID);
sqlUpdate.setString(4, newScheduleID);
sqlUpdate.setInt(5, newPrice);
sqlUpdate.setInt(6, TicketNo);
int rslt = sqlUpdate.executeUpdate();
System.err.println("Result code from Update: " + rslt);
if (rslt == 0) {
return false;
} else {
return true;
}
}
public static void main(String[] args) {
Update app = new Update();
boolean connOK = app.connect();
if (connOK) {
try {
app.updateTicket();
app.displayAll(); // from superclass
app.connection.close();
System.out.println("\nConnection closed. Goodbye.");
} catch (SQLException ex) {
System.err.println("There was a problem with the DB.");
}
}
}
}
努力解决为什么数据库与访问的链接带来了错误。似乎无法更新数据库中的字段。