?? shopmanagesystembean.java
字號:
package shopmanagesystem;/** * Title: 商店購物管理系統的完整實現 * Description: 教學示范 * Copyright: Copyright (c) 2003 * Company: 北京師范大學計算機系 * @author 孫一林 * @version 1.0 */import java.io.*;import java.util.*;import java.sql.*;public class shopManageSystemBean { private Connection connection = null; //定義與數據庫進行連接的Connection對象 private Statement statement = null; //定義查詢數據庫的Statement對象 private ResultSet rs = null; //定義數據庫查詢的結果集 private String name; //定義登錄系統的用戶名 private String password; //定義登錄系統的用戶密碼 public shopManageSystemBean() { } public void setUserInfo(String name, String password) //當用戶登錄成功后記錄用戶的用戶名和密碼 { this.name = name; this.password = password; } public String getUserName() //獲取用戶登錄的用戶名 { return this.name; } public String getUserPassword() //獲取用戶登錄的用戶密碼 { return this.password; } public boolean isNameExisted(String name) //判斷用戶登錄使用的用戶名是否存在 { boolean existed = false; try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //指定與數據庫連接使用JDBC-ODBC橋驅動程序 String url = "jdbc:odbc:shop"; //指定數據源名 connection = DriverManager.getConnection(url); //與數據源建立連接 String sql = "select * from manager_table where name='" + name + "'"; //創建獲取用戶名的SQL語句 Statement statement = connection.createStatement(); //創建Statement接口實例 ResultSet rs = statement.executeQuery(sql); //將數據存入結果集中 if(rs.next()) //如果結果集不為空,則用戶登錄使用的用戶名存在 { existed = true; } } catch(SQLException ex){ //捕捉異常 System.out.println("\nERROR:----- SQLException -----\n"); while (ex != null) { System.out.println("Message: " + ex.getMessage()); System.out.println("SQLState: " + ex.getSQLState()); System.out.println("ErrorCode: " + ex.getErrorCode()); ex = ex.getNextException(); } } catch(Exception ex ) { ex.printStackTrace(); } return existed; } public boolean isPassRight(String name, String password) //判斷用戶登錄使用的用戶密碼是否正確 { boolean isRight = false; try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //指定與數據庫連接使用JDBC-ODBC橋驅動程序 String url = "jdbc:odbc:shop"; //指定數據源名 connection = DriverManager.getConnection(url); //與數據源建立連接 String sql = "select * from manager_table where name='" + name + "' and password='" + password + "'"; //創建獲取相對于用戶名的用戶密碼的SQL語句 Statement statement = connection.createStatement(); //創建Statement接口實例 ResultSet rs = statement.executeQuery(sql); //將數據存入結果集中 if(rs.next()) //如果結果集不為空,則用戶登錄使用的用戶密碼正確 { isRight = true; } } catch(SQLException ex){ //捕捉異常 System.out.println("\nERROR:----- SQLException -----\n"); while (ex != null) { System.out.println("Message: " + ex.getMessage()); System.out.println("SQLState: " + ex.getSQLState()); System.out.println("ErrorCode: " + ex.getErrorCode()); ex = ex.getNextException(); } } catch(Exception ex ) { ex.printStackTrace(); } return isRight; } public ResultSet getProductSold() //獲取售出物品記錄的結果集 { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //指定與數據庫連接使用JDBC-ODBC橋驅動程序 String url = "jdbc:odbc:shop"; //指定數據源名 connection = DriverManager.getConnection(url); //與數據源建立連接 String sql = "select sell_table.product_id,sell_table.product_price,sell_table.sell_number,sell_table.sell_profit,product_table.product_name from sell_table,product_table where sell_table.product_id=product_table.product_id"; //創建獲取售出商品記錄的SQL語句 statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY); //創建Statement接口實例 rs = statement.executeQuery(sql); //將數據存入結果集中 } catch(SQLException ex){ //捕捉異常 System.out.println("\nERROR:----- SQLException -----\n"); while (ex != null) { System.out.println("Message: " + ex.getMessage()); System.out.println("SQLState: " + ex.getSQLState()); System.out.println("ErrorCode: " + ex.getErrorCode()); ex = ex.getNextException(); } } catch(Exception ex ) { ex.printStackTrace(); } return rs; } public void delSoldProduct(int id) //刪除指定的售出商品記錄 { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //指定與數據庫連接使用JDBC-ODBC橋驅動程序 String url = "jdbc:odbc:shop"; //指定數據源名 connection = DriverManager.getConnection(url); //與數據源建立連接 String sql = "delete from sell_table where product_id=" + id; //創建刪除指定ID號的售出商品記錄的SQL語句 statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); //創建Statement接口實例 statement.executeUpdate(sql); //執行刪除記錄命令 } catch(SQLException ex){ //捕捉異常 System.out.println("\nERROR:----- SQLException -----\n"); while (ex != null) { System.out.println("Message: " + ex.getMessage()); System.out.println("SQLState: " + ex.getSQLState()); System.out.println("ErrorCode: " + ex.getErrorCode()); ex = ex.getNextException(); } } catch(Exception ex ) { ex.printStackTrace(); } } public ResultSet getDealInfo() //獲取顧客交易記錄的結果集 { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //指定與數據庫連接使用JDBC-ODBC橋驅動程序 String url = "jdbc:odbc:shop"; //指定數據源名 connection = DriverManager.getConnection(url); //與數據源建立連接 String sql = "select cart_table.product_id,cart_table.buy_number,cart_table.customer_name,cart_table.customer_address"; sql+=",product_table.product_name from cart_table,product_table where cart_table.product_id=product_table.product_id"; //創建獲取顧客交易記錄的SQL語句 statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY); //創建Statement接口實例 rs = statement.executeQuery(sql); //將數據存入結果集中 } catch(SQLException ex){ //捕捉異常 System.out.println("\nERROR:----- SQLException -----\n"); while (ex != null) { System.out.println("Message: " + ex.getMessage()); System.out.println("SQLState: " + ex.getSQLState()); System.out.println("ErrorCode: " + ex.getErrorCode()); ex = ex.getNextException(); } } catch(Exception ex ) { ex.printStackTrace(); } return rs; } public ResultSet getSingleDeal(String customerName) //獲取指定顧客的交易記錄的結果集 { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //指定與數據庫連接使用JDBC-ODBC橋驅動程序 String url = "jdbc:odbc:shop"; //指定數據源名 connection = DriverManager.getConnection(url); //與數據源建立連接 String sql = "select cart_table.product_id,cart_table.buy_number,cart_table.customer_name,cart_table.customer_address"; sql+=",product_table.product_name from cart_table,product_table where cart_table.product_id=product_table.product_id and cart_table.customer_name='" + customerName + "'"; //創建獲取指定顧客交易記錄的SQL語句 statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY); //創建Statement接口實例 rs = statement.executeQuery(sql); //將數據存入結果集中 } catch(SQLException ex){ //捕捉異常 System.out.println("\nERROR:----- SQLException -----\n"); while (ex != null) { System.out.println("Message: " + ex.getMessage()); System.out.println("SQLState: " + ex.getSQLState()); System.out.println("ErrorCode: " + ex.getErrorCode()); ex = ex.getNextException();
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -