📄 store03.java
字号:
// com/wrox/connections/Store03.java
package connections;
import java.sql.*;
/**
* A class to represent a retail store in the Music system.
*/
public class Store03 {
int storeId;
String storeDescription = "";
int storeTypeId;
String storeAddress1 = "";
String storeAddress2 = "";
String storeCity = "";
String storePostalCode = "";
Connection connection;
Statement statement;
ResultSet resultSet;
// constructor
public Store03() {}
// constructor
public Store03(String description, int typeId, String address1,
String address2, String city, String postalCode) {
storeDescription = description;
storeTypeId = typeId;
storeAddress1 = address1;
storeAddress2 = address2;
storeCity = city;
storePostalCode = postalCode;
}
private Statement getStatement() throws SQLException {
connection = ConnectionFactory.getConnection();
return connection.createStatement();
}
/**
* Update the Stores table entry for this object. This method
* updates every column for the given store, regardless of whether
* the data was actually changed or not.
*/
public boolean update() {
int result = 0;
try {
statement = getStatement();
String sql = "update stores set " +
"StoreDescription='" + getStoreDescription() + "', " +
"StoreTypeId=" + getStoreTypeId() + ", " +
"StoreAddress1='" + getStoreAddress1() + "', " +
"StoreAddress2='" + getStoreAddress2() + "', " +
"StoreCity='" + getStoreCity() + "', " +
"StorePostalCode='" + getStorePostalCode() + "' " +
"where StoreID=" + getStoreId();
result = statement.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
} finally {
close();
}
return (result == 1);
}
/**
* Create an entry in the database for this object.
*/
public boolean create() {
int result = 0;
try {
statement = getStatement();
String sql = "insert into stores "
+ "(StoreDescription, StoreTypeID, StoreAddress1, "
+ "StoreAddress2, StoreCity, StorePostalCode) "
+ "values ('" + storeDescription + "', " + storeTypeId
+ ", " + "'" + storeAddress1 + "', " + "'"
+ storeAddress2 + "', " + "'" + storeCity + "', " + "'"
+ storePostalCode + "')";
result = statement.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
}
finally {
close();
}
return (result == 1);
}
/**
* Retrieve the entry for the given store id from the database. Return
* true if the entry is found. Return false if the entry is not
* found in the database.
*/
public boolean findByPrimaryKey(String id) {
if (id == null || id.equals("")) {
return false;
}
boolean result = false;
try {
statement = getStatement();
String sql = "select * from stores " + "where StoreID=" + id;
resultSet = statement.executeQuery(sql);
if (resultSet.next()) {
result = true;
setStoreId(resultSet.getInt("StoreID"));
storeDescription = resultSet.getString("StoreDescription");
storeTypeId = resultSet.getInt("StoreTypeID");
storeAddress1 = resultSet.getString("StoreAddress1");
storeAddress2 = resultSet.getString("StoreAddress2");
storeCity = resultSet.getString("StoreCity");
storePostalCode = resultSet.getString("StorePostalCode");
}
} catch (Exception e) {
e.printStackTrace();
}
finally {
close();
}
return result;
}
void close() {
ConnectionFactory.close(resultSet);
ConnectionFactory.close(statement);
ConnectionFactory.close(connection);
}
public String toString() {
return "Name : " + storeDescription + "\n" + "Store Id : "
+ storeId + "\n" + "Address : " + storeAddress1 + "\n"
+ " : " + storeAddress2 + "\n" + " : "
+ storeCity + ", " + storePostalCode;
}
public int getStoreId() {
return storeId;
}
private void setStoreId(int v) {
this.storeId = v;
}
public String getStoreDescription() {
return storeDescription;
}
public void setStoreDescription(String v) {
storeDescription = v;
}
public String getStoreAddress1() {
return storeAddress1;
}
public void setStoreAddress1(String v) {
storeAddress1 = v;
}
public String getStoreAddress2() {
return storeAddress2;
}
public void setStoreAddress2(String v) {
storeAddress2 = v;
}
public int getStoreTypeId() {
return storeTypeId;
}
public void setStoreTypeId(int v) {
storeTypeId = v;
}
public String getStoreCity() {
return storeCity;
}
public void setStoreCity(String v) {
storeCity = v;
}
public String getStorePostalCode() {
return storePostalCode;
}
public void setStorePostalCode(String v) {
storePostalCode = v;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -