📄 dbhelper.java~24~
字号:
package bookstore.util;import bookstore.ejb.*;import java.sql.*;import javax.sql.*;/** * This class contains helper methods for components * that access Cloudscape databases. */public final class DBHelper{ // The getNext<name>Id methods retrieve the next unique // primary key. public static final String getNextCustomerID(Connection con) throws SQLException { String customerID; int i; System.out.println("DBHelper getNextAccountId"); customerID=getNextId(con, "CustomerID"); i=8-customerID.length();//设定ID长度为10,"CT" 已经占去两位,还有8位,减去整数部分的长度, //位数全填零剩下的 while(i>0) { customerID="0"+customerID; i=i-1; }//while customerID="CT"+customerID; return customerID; } // getNextCustomerId public static final String getNextBasketID(Connection con) throws SQLException { String basketID; int i; System.out.println("DBHelper getNextBasketID"); basketID=getNextId(con, "BasketID"); i=8-basketID.length();//设定ID长度为10,"CT" 已经占去两位,还有8位,减去整数部分的长度, //位数全填零剩下的 while(i>0) { basketID="0"+basketID; i=i-1; }//while basketID="BK"+basketID; return basketID; } // getNextBasketId public static final String getNextCommentID(Connection con) throws SQLException { String commentID; int i; System.out.println("DBHelper getNextCommentID"); commentID=getNextId(con, "CommentID"); i=8-commentID.length();//设定ID长度为10,"CT" 已经占去两位,还有8位,减去整数部分的长度, //位数全填零剩下的 while(i>0) { commentID="0"+commentID; i=i-1; }//while commentID="CM"+commentID; return commentID; } // getNextCommentId public static final String getNextManagerID(Connection con) throws SQLException { String managerID; int i; System.out.println("DBHelper getNextmanagerID"); managerID=getNextId(con, "managerID"); i=8-managerID.length();//设定ID长度为10,"CT" 已经占去两位,还有8位,减去整数部分的长度, //位数全填零剩下的 while(i>0) { managerID="0"+managerID; i=i-1; }//while managerID="MG"+managerID; return managerID; } // getNextmanagerId private static final String getNextId(Connection con, String field) throws SQLException { System.out.println("DBHelper getNextId"); String selectStatement = "select "+field+" from MaxIDs"; System.out.println(selectStatement); String updateStatement = "update MaxIDs " + "set "+ field + " = "+ field + " +1 "; System.out.println(updateStatement); PreparedStatement prepSelect = con.prepareStatement(selectStatement); PreparedStatement prepUpdate = con.prepareStatement(updateStatement); prepUpdate.executeUpdate(); ResultSet rs = prepSelect.executeQuery(); rs.next(); int i = rs.getInt(1); rs.close(); prepSelect.close(); prepUpdate.close(); if (i <= 0) { throw new SQLException ("Field "+field+" in table MaxIDs is empty."); } return Integer.toString(i); } // getNextId} // class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -