navigation.java
来自「一个用java编写的功能强大的OA系统」· Java 代码 · 共 285 行
JAVA
285 行
package cn.js.fan.module.nav;import java.sql.*;import java.util.*;import cn.js.fan.base.*;import cn.js.fan.db.*;import cn.js.fan.util.*;import cn.js.fan.web.*;import org.apache.log4j.*;import java.io.Serializable;public class Navigation implements ITagSupport,Serializable { String name; String connname; String link; int orders; String newName; transient Logger logger = Logger.getLogger(Navigation.class.getName()); final String INSERT = "insert nav (name, link, orders, color) values (?,?,?,?)"; final String STORE = "update nav set name=?,link=?,color=? where name=?"; final String LOAD = "select name,link,orders,color from nav where name=?"; final String GETMAXORDERS = "select max(orders) from nav"; public Navigation() { connname = Global.defaultDB; if (connname.equals("")) logger.info("Directory:默认数据库名为空!"); } public Navigation(String name) { connname = Global.defaultDB; if (connname.equals("")) logger.info("Directory:默认数据库名为空!"); this.name = name; load(name); } public String getName() { return name; } public int getOrders() { return orders; } public void setOrders(int o) { this.orders = o; } public void setNewName(String n) { this.newName = n; } public void setName(String name) { this.name = name; } public String get(String field) { if (field.equals("name")) return name; else if (field.equals("link")) return link; else if (field.equals("orders")) return "" + orders; else return ""; } public String getLink() { return this.link; } public String getColor() { return color; } public void setLink(String lk) { this.link = lk; } public void setColor(String color) { this.color = color; } public boolean insert(String name, String link, String color) { orders = getMaxOrders() + 1; Conn conn = new Conn(connname); boolean re = false; try { PreparedStatement pstmt = conn.prepareStatement(INSERT); pstmt.setString(1, name); pstmt.setString(2, link); pstmt.setInt(3, orders); pstmt.setString(4, color); re = conn.executePreUpdate() == 1 ? true : false; } catch (Exception e) { logger.error(e.getMessage()); } finally { if (conn != null) { conn.close(); conn = null; } } return re; } public boolean store() { Conn conn = new Conn(connname); boolean re = false; try { PreparedStatement pstmt = conn.prepareStatement(STORE); pstmt.setString(1, newName); pstmt.setString(2, link); pstmt.setString(3, color); pstmt.setString(4, name); re = conn.executePreUpdate() == 1 ? true : false; } catch (SQLException e) { logger.error(e.getMessage()); } finally { if (conn != null) { conn.close(); conn = null; } } return re; } public void load(String navname) { Conn conn = new Conn(connname); ResultSet rs = null; try { PreparedStatement pstmt = conn.prepareStatement(LOAD); pstmt.setString(1, navname); rs = conn.executePreQuery(); if (rs != null) { if (rs.next()) { name = rs.getString(1); link = rs.getString(2); orders = rs.getInt(3); color = rs.getString(4); } } } catch (SQLException e) { logger.error(e.getMessage()); } finally { if (rs != null) { try { rs.close(); } catch (Exception e) {} rs = null; } if (conn != null) { conn.close(); conn = null; } } } public boolean del() { Conn conn = new Conn(connname); boolean re = false; try { String sql = "delete from nav where name=" + StrUtil.sqlstr(name); String sql1 = "update nav set orders=orders-1 where orders>" + orders; conn.beginTrans(); conn.executeUpdate(sql); conn.executeUpdate(sql1); re = true; conn.commit(); } catch (SQLException e) { conn.rollback(); logger.error(e.getMessage()); } finally { if (conn != null) { conn.close(); conn = null; } } return re; } public int getMaxOrders() { Conn conn = new Conn(connname); ResultSet rs = null; int maxorders = -1; try { PreparedStatement pstmt = conn.prepareStatement(GETMAXORDERS); rs = conn.executePreQuery(); if (rs != null) { if (rs.next()) { maxorders = rs.getInt(1); } } } catch (SQLException e) { logger.error(e.getMessage()); } finally { if (conn != null) { conn.close(); conn = null; } } return maxorders; } public boolean move(String direction) { Conn conn = new Conn(connname); boolean re = false; try { if (direction.equals("up")) { if (orders == 0) return true; String sql = "update nav set orders=orders+1 where orders=" + (orders - 1); conn.executeUpdate(sql); sql = "update nav set orders=orders-1 where name=" + StrUtil.sqlstr(name); conn.executeUpdate(sql); re = true; } else { int maxorders = getMaxOrders(); if (orders == maxorders) { return true; } else { String sql = "update nav set orders=orders-1 where orders=" + (orders + 1); conn.executeUpdate(sql); sql = "update nav set orders=orders+1 where name=" + StrUtil.sqlstr(name); conn.executeUpdate(sql); } re = true; } } catch (Exception e) { logger.error(e.getMessage()); } finally { if (conn != null) { conn.close(); conn = null; } } return re; } public Vector getAllNavName() { String sql = "select name from nav order by orders"; Conn conn = new Conn(connname); ResultSet rs = null; Vector v = null; try { rs = conn.executeQuery(sql); if (rs!=null) { v = new Vector(); while (rs.next()) { String name = rs.getString(1); v.addElement("" + name); } } } catch (Exception e) { logger.error(e.getMessage()); } finally { if (conn!=null) { conn.close(); conn = null; } } return v; } private String color = "";}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?