📄 sectorda.java
字号:
package com.captainli.dboperation;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import javax.servlet.jsp.tagext.TryCatchFinally;
import com.captainli.bean.SectorBean;
import com.captainli.struts.form.SectorForm;
import com.captainli.util.GetConnection;
/**
* sector表数据库操作类
* @author CaptainLi
*
*/
public class SectorDA {
private Connection conn = GetConnection.getConn();
private PreparedStatement pstmt = null;
private Statement stmt = null;
private ResultSet rs = null;
/**
* 关闭数据库对象
*
*/
public void closeDB(){
try {
if(rs != null){
rs.close();
}
if(stmt != null){
stmt.close();
}
if(pstmt != null){
pstmt.close();
}
if(conn != null){
conn.close();
}
} catch (Exception e) {
e.getStackTrace();
}
}
/**
* 返回所有部门
*/
public ArrayList selectSector(){
ArrayList arry = new ArrayList();
SectorBean bean = null;
String sql = "select * from sector order by s_id";
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while(rs.next()){
bean = new SectorBean();
bean.setS_id(rs.getInt("s_id"));
bean.setS_name(rs.getString("s_name"));
arry.add(bean);
}
} catch (Exception e) {
// TODO: handle exception
}finally{
closeDB();
}
return arry;
}
/**
* 通过ID得到部门
* @param s_id
* @return
*/
public String selectSectorByID(int s_id){
String tmp = "";
String sql = "select s_name from sector where s_id = ?";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, s_id);
rs = pstmt.executeQuery();
if(rs.next()){
tmp = rs.getString("s_name");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
closeDB();
}
return tmp;
}
/**
* 添加部门
*/
public void addSector(SectorForm form){
String sql = "insert into sector values (?)";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, form.getS_name());
pstmt.execute();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
closeDB();
}
}
/**
* 修改部门
*/
public void updateSector(SectorForm form, int s_id){
String sql = "update sector set s_name = ? where s_id = ?";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, form.getS_name());
pstmt.setInt(2, s_id);
pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
closeDB();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -