📄 recording.java
字号:
package transactions;
import java.sql.*;
import java.math.*;
import java.util.*;
import statements.*;
import connections.*;
public class Recording {
int recordingId;
String recordingTitle;
int publisherId;
String catalogNumber;
int recordingFormatId;
String releaseDate;
int languageId;
BigDecimal price;
String coverImageFileSpec;
Connection connection;
static String insertSql =
"INSERT INTO RECORDINGS "
+ "(RECORDINGTITLE, PUBLISHERID, CATALOGNUMBER, RECORDINGFORMATID, "
+ "RELEASEDATE, LANGUAGEID, LISTPRICE, COVERIMAGEFILESPEC) "
+ "VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
static String queryIdSql = "SELECT RECORDINGID FROM RECORDINGS "
+ "WHERE CATALOGNUMBER=?";
public boolean create(Properties data) {
boolean result = true;
try {
getConnection(false);
insertRecording(data);
recordingId = getRecordingId();
insertTracks(data);
connection.commit();
System.out.println("All data has been entered and committed");
} catch (Exception e) {
e.printStackTrace();
try {
connection.rollback();
} catch (SQLException e2) {
e2.printStackTrace();
}
result = false;
}
finally {
ConnectionFactory.close(connection);
}
return result;
}
void insertRecording(Properties data)
throws SQLException, NumberFormatException {
PreparedStatement ps = getPreparedStatement(insertSql);
recordingTitle = data.getProperty("recordingtitle");
ps.setString(1, recordingTitle);
publisherId = Integer.parseInt(data.getProperty("publisherid"));
ps.setInt(2, publisherId);
catalogNumber = data.getProperty("catalognumber");
ps.setString(3, catalogNumber);
recordingFormatId =
Integer.parseInt(data.getProperty("recordingformatid"));
ps.setInt(4, recordingFormatId);
releaseDate = data.getProperty("releasedate");
ps.setString(5, releaseDate);
languageId = Integer.parseInt(data.getProperty("languageid"));
ps.setInt(6, languageId);
price = new BigDecimal(data.getProperty("price"));
ps.setDouble(7, price.doubleValue());
coverImageFileSpec = data.getProperty("coverimagefilespec");
ps.setString(8, coverImageFileSpec);
ps.executeUpdate();
ConnectionFactory.close(ps);
}
int getRecordingId() throws SQLException {
PreparedStatement ps = getPreparedStatement(queryIdSql);
ps.setString(1, catalogNumber);
ResultSet rs = ps.executeQuery();
rs.next();
int result = rs.getInt(1);
ConnectionFactory.close(rs);
ConnectionFactory.close(ps);
return result;
}
void insertTracks(Properties data) throws SQLException {
int numTracks = Integer.parseInt(data.getProperty("numberoftracks"));
Track track = new Track();
PreparedStatement ps = getPreparedStatement(track.getCreateSql());
track.setCreateStatement(ps);
track.setRecordingId(recordingId);
for (int i = 0; i < numTracks; i++) {
System.out.println("Entering data for track " + (i + 1));
String trackData = data.getProperty("track" + i);
StringTokenizer st = new StringTokenizer(trackData, ",");
track.setTrackNumber(i + 1);
track.setTrackTitle(st.nextToken());
track.setArtistId(Integer.parseInt(st.nextToken()));
track.setStyleId(Integer.parseInt(st.nextToken()));
track.setSampleFilespec("");
track.executeSql();
}
}
void getConnection(boolean autocommit) throws SQLException {
connection = ConnectionFactory.getConnection();
connection.setAutoCommit(autocommit);
}
PreparedStatement getPreparedStatement(String sql) throws SQLException {
return connection.prepareStatement(sql);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -