📄 track.java
字号:
package statements;
import java.sql.*;
import java.util.*;
import connections.ConnectionFactory;
public class Track {
//track data
int recordingId;
String recordingTitle;
int trackNumber;
String trackTitle;
int artistId;
String artistName;
int styleId;
String styleName;
String sampleFilespec;
//JDBC variables
PreparedStatement preparedStmt;
ResultSet resultSet;
Vector results;
//CRUD flags
boolean create;
boolean retrieve;
boolean update;
boolean delete;
static String sqlSelect = "SELECT " +
"recordings.recordingid, " +
"recordings.recordingtitle, " +
"tracks.tracknumber, " +
"tracks.tracktitle, " +
"tracks.artistid, " +
"artistsandperformers.artistname, " +
"tracks.styleid, " +
"audiostyles.audiostyledescription, " +
"tracks.samplefilespec ";
static String sqlFrom =
"FROM tracks, recordings, artistsandperformers, audiostyles ";
static String sqlWhere = "WHERE ";
static String sqlWhereCont =
"LIKE ? AND recordings.recordingid = tracks.recordingid AND " +
"artistsandperformers.artistid = tracks.artistid AND " +
"audiostyles.audiostyleid = tracks.styleid";
static String sqlInsert = "INSERT INTO TRACKS " +
"(recordingid, tracknumber, tracktitle, " +
"artistid, styleid, samplefilespec) VALUES (?, ?, ?, ?, ?, ?)";
//constructor
public Track() {}
// Returns the SQL INSERT statement used by this class for creating
// an entry in the database
public static String getCreateSql() {
return sqlInsert;
}
//Prepare the class for creating (inserting) a row in the
//database. The PreparedStatement should be created using the SQL
//string obtained by calling the method getCreateSql() in this
//class.
public void setCreateStatement(PreparedStatement ps) {
preparedStmt = ps;
create = (ps != null ? true : false);
retrieve = false;
update = false;
delete = false;
}
//methods to return differen SQL strings for searching
public static String getFindTrackTitleSql()
{
return sqlSelect + sqlFrom + sqlWhere +
"tracks.tracktitle " + sqlWhereCont;
}
public static String getFindRecordTitleSql()
{
return sqlSelect + sqlFrom + sqlWhere +
"recordings.recordingtitle " + sqlWhereCont;
}
public static String getFindArtistSql()
{
return sqlSelect + sqlFrom + sqlWhere +
"artistsandperformers.artistname " + sqlWhereCont;
}
//Prepare the class to query for rows in the database. The
//PreparedStatement should be created using the SQL string obtained
//by calling one of the getFindXXXSql() methods in this class.
public void setQueryStatement(PreparedStatement ps) {
preparedStmt = ps;
create = false;
retrieve = (ps != null ? true : false);
update = false;
delete = false;
}
//execute the PreparedStatement
private boolean executeSql() throws SQLException {
if (create || update || delete) {
int result = preparedStmt.executeUpdate();
return (result == 1);
} else if (retrieve) {
resultSet = preparedStmt.executeQuery();
results = new Vector();
if (resultSet.next()) {
Track track = new Track();
track.populate(resultSet);
results.add(track);
return true;
}
return false;
} else {
throw new SQLException("No PreparedStatement has been set.");
}
}
//Find tracks that match a TrackTitle search string
public Vector findByTrackTitle(String trackTitle)
throws SQLException
{
preparedStmt.setString(1, "%" + trackTitle + "%");
if (executeSql()) {
parseResultSet();
}
return results;
}
//Find tracks that match a Recording Title search string
public Vector findByRecordingTitle(String recordingTitle)
throws SQLException
{
preparedStmt.setString(1, "%" + recordingTitle + "%");
if (executeSql()) {
parseResultSet();
}
return results;
}
//Find tracks that match an Artist name search string
public Vector findByArtistName(String artistName)
throws SQLException
{
preparedStmt.setString(1, "%" + artistName + "%");
if (executeSql()) {
parseResultSet();
}
return results;
}
//Read each row in the ResultSet and create a Track instance from
//the row
private void parseResultSet() throws SQLException
{
try {
while (resultSet.next()) {
Track track = new Track();
track.populate(resultSet);
results.add(track);
}
} finally {
ConnectionFactory.close(resultSet);
}
}
//initialize the datamembers of this class using data from the
//current row of the ResultSet
public void populate(ResultSet rset) throws SQLException
{
setRecordingId(rset.getInt("RECORDINGID"));
setRecordingTitle(rset.getString("RECORDINGTITLE"));
setTrackNumber(rset.getInt("TRACKNUMBER"));
setTrackTitle(rset.getString("TRACKTITLE"));
setArtistId(rset.getInt("ARTISTID"));
setArtistName(rset.getString("ARTISTNAME"));
setStyleId(rset.getInt("STYLEID"));
setStyleName(rset.getString("AUDIOSTYLEDESCRIPTION"));
setSampleFilespec(rset.getString("SAMPLEFILESPEC"));
}
//Setters and Getters for the member variables
public int getRecordingId() { return recordingId; }
public void setRecordingId(int v) throws SQLException {
recordingId = v;
}
public int getTrackNumber() { return trackNumber; }
public void setTrackNumber(int v) throws SQLException {
trackNumber = v;
}
public String getTrackTitle() { return trackTitle; }
public void setTrackTitle(String v) throws SQLException {
trackTitle = v;
}
public int getArtistId() { return artistId; }
public void setArtistId(int v) throws SQLException {
artistId = v;
}
public int getStyleId() { return styleId; }
public void setStyleId(int v) throws SQLException {
styleId = v;
}
public String getSampleFilespec() { return sampleFilespec; }
public void setSampleFilespec(String v) throws SQLException {
sampleFilespec = v;
}
public String getRecordingTitle() { return recordingTitle; }
public void setRecordingTitle(String v) {
recordingTitle = v;
}
public String getStyleName() { return styleName; }
public void setStyleName(String v) {
styleName = v;
}
public String getArtistName() { return artistName; }
public void setArtistName(String v) {
artistName = v;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -