📄 storeds.java
字号:
package connections;
import java.sql.*;
import javax.sql.*;
import datasource.*;
/**
* A class to represent a retail store in the Music system.
*/
public class StoreDS {
int storeId;
String storeDescription = "";
int storeTypeId;
String storeAddress1 = "";
String storeAddress2 = "";
String storeCity = "";
String storePostalCode = "";
DataSourceClient client;
Connection connection;
Statement statement;
ResultSet resultSet;
public StoreDS() {
this("", 0, "", "", "", "");
}
public StoreDS(String description, int typeId, String address1,
String address2, String city, String postalCode) {
client = new DataSourceClient();
storeDescription = description;
storeTypeId = typeId;
storeAddress1 = address1;
storeAddress2 = address2;
storeCity = city;
storePostalCode = postalCode;
}
private Statement getStatement() throws SQLException {
connection = client.getConnection();
return connection.createStatement();
}
public boolean create() {
int result = 0;
try {
statement = getStatement();
String sql = "insert into stores " +
"(StoreId, StoreDescription, StoreTypeID, StoreAddress1, " +
"StoreAddress2, StoreCity, StorePostalCode) " + "values (" +
"Storeid_SEQ.NEXTVAL, " + "'" + storeDescription + "', " +
storeTypeId + ", " + "'" + storeAddress1 + "', " + "'" +
storeAddress2 + "', " + "'" + storeCity + "', " + "'" +
storePostalCode + "')";
result = statement.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
} finally {
close(statement);
close(connection);
}
return (result == 1);
}
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(resultSet);
close(statement);
close(connection);
}
return result;
}
private void close(ResultSet resultSet) {
try {
if (resultSet != null) {
resultSet.close();
}
} catch (Exception ignored) {
ignored.printStackTrace();
}
}
private void close(Statement statement) {
try {
if (statement != null) {
statement.close();
}
} catch (Exception ignored) {
ignored.printStackTrace();
}
}
private void close(Connection connection) {
try {
if (connection != null) {
connection.close();
}
} catch (Exception ignored) {
ignored.printStackTrace();
}
}
public String toString() {
return "Name : " + storeDescription + "\n" + "Store Id : " +
storeId + "\n" + "Address : " + storeAddress1 + "\n" +
" : " + storeAddress2 + "\n" + " : " +
storeCity + ", " + storePostalCode;
}
public int getStoreId() {
return storeId;
}
public 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 + -