📄 recording.java
字号:
setRecordingId(resultSet.getLong(1));
// Commit the transaction
databaseConnection.commit();
} finally {
// If we got a result set, close it
if (resultSet != null) {
resultSet.close();
}
// If we got a statement, close it
if (statement != null) {
statement.close();
}
if (statement2 != null) {
statement2.close();
}
// If we got a connection, close it
if (databaseConnection != null) {
databaseConnection.close();
}
}
}
/**
* Updates the non-key values of the current Recording object
* in the database.
*/
public void update() throws Exception {
Connection databaseConnection = null;
Statement statement = null;
try {
// Get a new connection and statement
databaseConnection = getConnection();
statement = databaseConnection.createStatement();
// Update the row
String updateSQL = "update RECORDINGS" + " set RECORDING_TITLE = '" +
getRecordingTitle() + "', " +
" RECORDING_ARTIST = '" + getRecordingArtist() +
"', " + " CATALOG_NUMBER = '" +
getCatalogNumber() + "', " + " LIST_PRICE = " +
getListPrice() + " where RECORDING_ID = " +
getRecordingId();
statement.executeUpdate(updateSQL);
} finally {
// If we got a statement, close it
if (statement != null) {
statement.close();
}
// If we got a connection, close it
if (databaseConnection != null) {
databaseConnection.close();
}
}
}
/**
* Deletes the current Recording object from the database.
*/
public void delete() throws Exception {
Connection databaseConnection = null;
Statement statement = null;
System.out.println("Delete called");
try {
// Get a new connection and statement
databaseConnection = getConnection();
statement = databaseConnection.createStatement();
// Insert the row
String deleteSQL = "delete from RECORDINGS " +
" where RECORDING_ID = " + getRecordingId();
System.out.println("SQL String set" + deleteSQL);
statement.executeUpdate(deleteSQL);
System.out.println("Delete SQL executed");
} finally {
// If we got a statement, close it
if (statement != null) {
statement.close();
}
// If we got a connection, close it
if (databaseConnection != null) {
databaseConnection.close();
}
}
}
// --------------------------------------------------------------
// Section 3: Methods to make the class self-contained
// --------------------------------------------------------------
/**
* Prints the contents of all variables.
*/
public String toString() {
return "Recording: " + getRecordingId() + "\n Title: " +
getRecordingTitle() + "\n Artist: " + getRecordingArtist() +
"\n Catalog Number: " + getCatalogNumber() +
"\n List Price: " + getListPrice();
}
/**
* Constant for the name of the JDBC Driver class connecting
* to the Oracle demo database.
*/
private static final String ORACLE_JDBC_DRIVER_CLASS_NAME =
"sun.jdbc.odbc.JdbcOdbcDriver";
/**
* Constant for the name of the URL for where the Oracle
* JDBC driver will connect to the database.
*/
private static final String ORACLE_DEMO_DB_URL =
"jdbc:odbc:MusicStore2";
/**
* Constants for the user to log into the Oracle demo database.
*/
//private static final String ORACLE_DEMO_DB_USERID_PROPERTY = "user";
// private static final String ORACLE_DEMO_DB_USERID = "beg";
/**
* Constants for the password to log into the Oracle demo database.
*/
// private static final String ORACLE_DEMO_DB_PASSWORD_PROPERTY = "password";
// private static final String ORACLE_DEMO_DB_PASSWORD = "java";
/**
* Helper method to retrieve a JDBC connection from
* the Oracle demo database. Note, this code must be called
* in a try block with a finally block which closes the connection.
*
* @throws Exception if the connection cannot be created
*
* @return JDBC Connection to the Oracle demo database if successful
*/
private static Connection getConnection() throws Exception {
// Create a new instance of the driver manager class, so
// it initializes itself for use and registers with the
// JDBC DriverManager
Class.forName(ORACLE_JDBC_DRIVER_CLASS_NAME).newInstance();
// Create connection properties object
Properties connectionProperties = new Properties();
/*connectionProperties.put(ORACLE_DEMO_DB_USERID_PROPERTY,
ORACLE_DEMO_DB_USERID);
connectionProperties.put(ORACLE_DEMO_DB_PASSWORD_PROPERTY,
ORACLE_DEMO_DB_PASSWORD);
*/
// create a connection
return DriverManager.getConnection(ORACLE_DEMO_DB_URL);
//connectionProperties);
}
/**
* Helper method to output a set of Recording objects
* in a Vector to System.out.
*/
private static void outputRecordings(Vector recordings) {
Recording recording;
Enumeration e = recordings.elements();
while (e.hasMoreElements()) {
recording = (Recording) e.nextElement();
// Print to output stream
System.out.println("\n" + recording);
}
}
/**
* Main method to test the capabilities above. CRUDs the
* database by creating 4 recordings, reading them back,
* updates the price on one and reads it back, and
* deletes all 4.
*/
public static void main(String[] args) {
Recording rec1, rec2, rec3, rec4;
try {
rec1 = new Recording("Cuts Like A Knife", "Bryan Adams",
"392-0000022", 11.99);
rec2 = new Recording("Working Class Dog", "Rick Springfield",
"RS-32-1133", 10.99);
rec3 = new Recording("Come And Join Us", "Petra", "303-9388293",
11.99);
rec4 = new Recording("Lucky Town", "Bruce Springsteen",
"BK-30-23993", 13.99);
// Create the recordings in the database
rec1.create();
rec2.create();
rec3.create();
rec4.create();
// Read the recordings back and output
System.out.println("\nYou should see 4 recordings:");
Vector springArtistRecordings = Recording.findByArtist("%Spring%");
outputRecordings(springArtistRecordings);
Vector recordingsStartWithC = Recording.findByTitle("C%");
outputRecordings(recordingsStartWithC);
// Update recording 1 price
rec1.setListPrice(7.99);
rec1.update();
// Read record back and make sure update made it
System.out.println("\nThis recording should have a price of 7.99");
Vector updatedRecording = Recording.findByArtist("Bryan Adams");
outputRecordings(updatedRecording);
// Delete all recordings
rec1.delete();
System.out.println("Deleted the first row");
rec2.delete();
System.out.println("Deleted the second rwo");
rec3.delete();
System.out.println("Deleted the third row");
rec4.delete();
System.out.println("Deleted the fourth row");
// Read all recordings back, shouldn't be any
System.out.println("\nDeleted all, shouldn't be any recordings:");
Vector allRecordings = Recording.findByArtist("%");
outputRecordings(allRecordings);
} catch (Exception e) {
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -