📄 sequenceutil.java
字号:
package employee;import java.sql.*;import javax.naming.NamingException;import java.util.*;import java.io.*;public class SequenceUtil{ //下面的函数产生序列器 public static String getSequence(String tablename) throws NamingException,SQLException { String sql = "SELECT REPLACE(LPAD(" + tablename + ".NEXTVAL,12),' ','0') FROM DUAL"; String sec = ""; Connection conn = null; try { conn = EjbCommon.getLocalConnection(); PreparedStatement ps = conn.prepareStatement(sql); ResultSet rs = ps.executeQuery(); while (rs.next()) { sec = rs.getString(1); } } catch (NamingException e) {throw e;} catch (SQLException e) {throw e;} finally { try { if (conn != null) {conn.close();} } catch (SQLException e1) {throw e1;} finally { } } return sec; } public static String getSequence() { long id = System.currentTimeMillis(); String seq = Long.toString(id); return seq; } public static String getUniteCode() { String code = ""; code = "ID_"; java.sql.Date dt = new java.sql.Date(System.currentTimeMillis()); code += dt.toString(); //而如果这么用的话,得到的是:ID_2003072404CB077B81059026110484。这才是我们想要的ID code += getPhysicalAddress().substring(4); code += getSequence(); int pos = 0; while (code.lastIndexOf("-") > 0) { pos = code.lastIndexOf("-"); code = code.substring(0, pos) + code.substring(pos + 1, code.length()); } return code; } //上面的函数产生序列器 //取得网卡的物理地址开始(说明:这一段代码是易建公司的人写的,值得研究) private static String _strPhysicalAddress; private static final int _physicalLength = 16; static public String getPhysicalAddress() { if (_strPhysicalAddress == null || _strPhysicalAddress.trim().equals("")) { String cmd = "cmd.exe /c ipconfig/all"; Vector result; result = execute(cmd); _strPhysicalAddress = parseCmd(result.toString()); } return _strPhysicalAddress; } //从字符串中解析出所需要获得的字符串 static private String parseCmd(String s) { String find = "Physical Address. . . . . . . . . :"; int findIndex = s.indexOf(find); if (findIndex == -1) return "no net adapter"; else return s.substring(findIndex + find.length() + 1,findIndex + find.length() + 2 + _physicalLength); } //运行ipconfig命令,并且自动捕捉输出。这一段代码值得研究! private static Process _p; public static Vector execute(String shellCommand) { try { Start(shellCommand); Vector vResult = new Vector(); DataInputStream in = new DataInputStream(_p.getInputStream()); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); String line; do { line = reader.readLine(); if (line == null) { break; } else { vResult.addElement(line); } } while (true); reader.close(); return vResult; } catch (IOException e) { return null; } } //开始执行 public static void Start(String shellCommand) throws IOException { try { if (_p != null) { kill(); } Runtime sys = Runtime.getRuntime(); _p = sys.exec(shellCommand); } catch (IOException e) { throw e; } } /** kill this process */ public static void kill() { if (_p != null) { _p.destroy(); _p = null; } } //取得网卡的物理地址结束 public SequenceUtil(){}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -