⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 store02.java

📁 此程序都是企业级 的数据库开发程序 全面揭示了JAVA对数据库的操作
💻 JAVA
字号:
// BegJavaDB/Ch06/connections/Store02.java
package connections;

import java.sql.*;

/**
 * A class to represent a retail store in the Music system.
 */
public class Store02 {
  int storeId;
  String storeDescription = "";
  int storeTypeId;
  String storeAddress1 = "";
  String storeAddress2 = "";
  String storeCity = "";
  String storePostalCode = "";

  Connection connection;
  Statement statement;
  ResultSet resultSet;

  // constructor
  public Store02() {}

  // constructor
  public Store02(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();
  } 

  /**
   * 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 + -