📄 navigation.java
字号:
package cn.js.fan.module.nav;
import java.io.Serializable;
import java.sql.*;
import java.util.Vector;
import cn.js.fan.base.ITagSupport;
import cn.js.fan.db.Conn;
import cn.js.fan.util.StrUtil;
import cn.js.fan.web.Global;
import org.apache.log4j.Logger;
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,target) values (?,?,?,?,?)";
final String STORE =
"update nav set name=?,link=?,color=?,target=? where name=?";
final String LOAD =
"select name,link,orders,color,target from nav where name=?";
final String GETMAXORDERS =
"select max(orders) from nav";
public Navigation() {
connname = Global.defaultDB;
if (connname.equals(""))
logger.info("Directory:connname is empty.");
}
public Navigation(String name) {
connname = Global.defaultDB;
if (connname.equals(""))
logger.info("Directory:connname is empty.");
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 String getTarget() {
return target;
}
public void setLink(String lk) {
this.link = lk;
}
public void setColor(String color) {
this.color = color;
}
public void setTarget(String target) {
this.target = target;
}
public boolean insert(String name, String link, String color, String target) {
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);
pstmt.setString(5, target);
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, target);
pstmt.setString(5, 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);
target = rs.getString(5);
}
}
} 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) {
// logger.info(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 = "";
private String target;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -