📄 attacksolutiondao.java
字号:
* 该Collection集合中会有多个AttackSolution对象
*/
public Collection retrieveOffsetAttackSolution(int offset,int limit) {
// 提供一个创建预编译SQL 语句的变量
PreparedStatement ps = null;
// 提供一个返回SQL查询结果的ResultSet接口变量
// ResultSet带有游标可以指向返回结果中的某条记录
ResultSet rs = null;
// 实现Collection接口的ArrayList类,创建该类的实例作为本方法的返回
ArrayList list = new ArrayList();
// SQL语句“无条件查询fw_attacksolution表按attack_event_code排序”
String sql = "SELECT attack_event_code,attack_mean, attack_action from fw_attacksolution order by attack_event_code";
// JDBC连接
Connection con = null;
try {
// 取得JDBC连接
con = getConnection();
// 判断JDBC连接是否被关闭,若关闭则直接抛出一个IllegalStateException
if (con.isClosed()) {
throw new IllegalStateException("ERROR.THE CONNECTION ISCLOSED");
}
// 预编译SQL 语句并执行
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
// 当返回结果集中有记录时进行处理
this.rsHandler(rs,offset,limit);
int countrow=0 ;
while ((countrow++<limit)&&rs.next()) {
// 定义AttackSolution类型的变量并实例化
// 在这里不将其放在循环外进行实例化的原因请读者自己考虑一下
AttackSolution attackSolution = new AttackSolution();
attackSolution.setAttack_event_code(rs.getString(1));
attackSolution.setAttack_mean(rs.getString(2));
attackSolution.setAttack_action(rs.getString(3));
list.add(attackSolution);
}
// 捕捉SQLException
} catch (SQLException ex) {
// 在控制台将SQLException打印出来方便调试
ex.printStackTrace();
} finally {
// 在完成整段代码的工作后必须将JDBC连接关闭
try {
if (ps != null) {
ps.close();
con.close();
}
// 注意这里还要关闭ResultSet的接口变量
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("ERROR.UNEXPECTED");
}
}
// 返回list
return list;
}
/**
* 将攻击解决方案Table按条件显示出来 本方法提供三个String类型的输入参数分别对应查询条件:
* argAttackEventCode,argAttackEventMean,argAttackAction
* 本方法返回一个Collection类型的结果 该Collection集合中会有多个AttackSolution对象
*/
public Collection retrieveAllAttackSolution(String argAttackEventCode,
String argAttackEventMean, String argAttackAction) {
// 提供一个创建预编译SQL 语句的变量
PreparedStatement ps = null;
// 提供一个返回SQL查询结果的ResultSet接口变量
// ResultSet带有游标可以指向返回结果中的某条记录
ResultSet rs = null;
// 实现Collection接口的ArrayList类,创建该类的实例作为本方法的返回
ArrayList list = new ArrayList();
// 利用编码技巧拼写SQL条件语句
String conditionSQL = "";
String whereSQL = " WHERE ";
// 当传入的变量值为空或者null时,不需要where语句
if ((argAttackEventCode == null || argAttackEventCode.equals(""))
&& (argAttackEventMean == null || argAttackEventMean.equals(""))
&& (argAttackAction == null || argAttackAction.equals(""))) {
whereSQL = "";
}
// 当传入的变量值存在时,需要拼成条件SQL
if (argAttackEventCode != null && !argAttackEventCode.equals("")) {
conditionSQL = conditionSQL + " attack_event_code like '%"
+ argAttackEventCode + "%' AND ";
}
if (argAttackEventMean != null && !argAttackEventMean.equals("")) {
conditionSQL = conditionSQL + " attack_mean like '%"
+ argAttackEventMean + "%' AND ";
}
if (argAttackAction != null && !argAttackAction.equals("")) {
conditionSQL = conditionSQL + " attack_action like '%"
+ argAttackAction + "%' AND ";
}
// 去掉条件最后一个AND
if (!conditionSQL.equals("")) {
conditionSQL = conditionSQL.substring(0, conditionSQL.length() - 4);
}
// SQL语句“按输入条件查询fw_attacksolution表并按attack_event_code排序”
String sql = "SELECT attack_event_code,attack_mean, attack_action from fw_attacksolution "
+ whereSQL + conditionSQL + " order by attack_event_code";
// JDBC连接
Connection con = null;
try {
// 取得JDBC连接
con = getConnection();
// 判断JDBC连接是否被关闭,若关闭则直接抛出一个IllegalStateException
if (con.isClosed()) {
throw new IllegalStateException("ERROR.THE CONNECTION ISCLOSED");
}
// 预编译SQL 语句并执行
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
// 当返回结果集中有记录时进行处理
while (rs.next()) {
// 定义AttackSolution类型的变量并实例化
// 在这里不将其放在循环外进行实例化的原因请读者自己考虑一下
AttackSolution attackSolution = new AttackSolution();
attackSolution.setAttack_event_code(rs.getString(1));
attackSolution.setAttack_mean(rs.getString(2));
attackSolution.setAttack_action(rs.getString(3));
list.add(attackSolution);
}
// 捕捉SQLException
} catch (SQLException ex) {
// 在控制台将SQLException打印出来方便调试
ex.printStackTrace();
} finally {
// 在完成整段代码的工作后必须将JDBC连接关闭
try {
if (ps != null) {
ps.close();
con.close();
}
// 注意这里还要关闭ResultSet的接口变量
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("ERROR.UNEXPECTED");
}
}
// 返回list
return list;
}
public int getrows() {
// 提供一个创建预编译SQL 语句的变量
PreparedStatement ps = null;
// 提供一个返回SQL查询结果的ResultSet接口变量
// ResultSet带有游标可以指向返回结果中的某条记录
ResultSet rs = null;
// SQL语句“无条件查询fw_attacksolution表按attack_event_code排序”
String sql = "SELECT count(*) from fw_attacksolution order by attack_event_code";
// JDBC连接
Connection con = null;
int rowscount = 0;
try {
// 取得JDBC连接
con = getConnection();
// 判断JDBC连接是否被关闭,若关闭则直接抛出一个IllegalStateException
if (con.isClosed()) {
throw new IllegalStateException("ERROR.THE CONNECTION ISCLOSED");
}
// 预编译SQL 语句并执行
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
// 当返回结果集中有记录时进行处理
if (rs.next()) {
rowscount = rs.getInt(1);
} else {
throw new RuntimeException("ERROR.NOTFOUNDANYROW");
}
return rowscount;
// 捕捉SQLException
} catch (SQLException ex) {
// 在控制台将SQLException打印出来方便调试
ex.printStackTrace();
return 0;
} finally {
// 在完成整段代码的工作后必须将JDBC连接关闭
try {
if (ps != null) {
ps.close();
con.close();
}
// 注意这里还要关闭ResultSet的接口变量
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("ERROR.UNEXPECTED");
}
}
}
public void rsHandler(ResultSet rs, int offset, int limit) {
int count = 0;
try {
rs.absolute(-1);
count = rs.getRow();
if (offset <= 0) {
rs.beforeFirst();
} else {
rs.absolute(offset);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -