?? shopmanagesystembean.java
字號:
} } catch(Exception ex ) { ex.printStackTrace(); } return rs; } public void delSingleCustomerDeal(String customerName) //刪除指定顧客的交易記錄 { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //指定與數據庫連接使用JDBC-ODBC橋驅動程序 String url = "jdbc:odbc:shop"; //指定數據源名 connection = DriverManager.getConnection(url); //與數據源建立連接 String sql = "delete from cart_table where customer_name='" + customerName + "'"; //創建刪除指定顧客的交易記錄的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 getProductList() //獲取庫存物品記錄的結果集 { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //指定與數據庫連接使用JDBC-ODBC橋驅動程序 String url = "jdbc:odbc:shop"; //指定數據源名 connection = DriverManager.getConnection(url); //與數據源建立連接 String sql = "select * from product_table"; //創建獲取庫存商品記錄的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 insertNewProduct(int id, String name, int price, int quantity) //向庫存商品中插入新商品記錄 { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //指定與數據庫連接使用JDBC-ODBC橋驅動程序 String url = "jdbc:odbc:shop"; //指定數據源名 connection = DriverManager.getConnection(url); //與數據源建立連接 String sql = "insert into product_table values("+id+",'"+name+"',"+price+","+quantity+")"; //創建向庫存商品中插入新商品記錄的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 boolean isProductExistedInOtherTable(int id) //判斷售出商品表和顧客交易表中是否存在涉及到庫存商品表中的商品信息 { boolean isExisted = false; try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //指定與數據庫連接使用JDBC-ODBC橋驅動程序 String url = "jdbc:odbc:shop"; //指定數據源名 connection = DriverManager.getConnection(url); //與數據源建立連接 String sql = "select * from sell_table where product_id=" + id; //創建獲取指定ID號的售出商品記錄的SQL語句 statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY); //創建Statement接口實例 rs = statement.executeQuery(sql); //將數據存入結果集中 if(rs.next()) //如果結果集不為空,則售出商品表中存在涉及到庫存商品表中的商品信息 { isExisted = true; return isExisted; } sql = "select * from cart_table where product_id=" + id; //創建獲取指定ID號的交易商品記錄的SQL語句 statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY); //創建Statement接口實例 rs = statement.executeQuery(sql); //將數據存入結果集中 if(rs.next()) //如果結果集不為空,則顧客交易表中存在涉及到庫存商品表中的商品信息 { isExisted = true; return isExisted; } } 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 isExisted; } public void delProduct(int id) //刪除指定的庫存商品記錄 { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //指定與數據庫連接使用JDBC-ODBC橋驅動程序 String url = "jdbc:odbc:shop"; //指定數據源名 connection = DriverManager.getConnection(url); //與數據源建立連接 String sql = "delete from product_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 getProductInfo(int id) //獲取指定的庫存商品記錄的結果集 { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //指定與數據庫連接使用JDBC-ODBC橋驅動程序 String url = "jdbc:odbc:shop"; //指定數據源名 connection = DriverManager.getConnection(url); //與數據源建立連接 String sql = "select * from product_table where product_id=" + id; //創建獲取指定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 updateProduct(int id, String name, float price, int quantity) //更新指定的庫存商品記錄信息 { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //指定與數據庫連接使用JDBC-ODBC橋驅動程序 String url = "jdbc:odbc:shop"; //指定數據源名 connection = DriverManager.getConnection(url); //與數據源建立連接 String sql = "update product_table set product_name='" + name + "', product_price=" + price + ",product_quantity=" + quantity +" 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(); } }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -