📄 depdao.java
字号:
package org.xjtu.bank.sysdao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import org.xjtu.bank.sysdb.DBAccess;
import org.xjtu.bank.sysvo.DepVo;
import org.xjtu.bank.sysvo.UnitVo;
public class DepDao
{
private Connection con;
private PreparedStatement stmt;
private DBAccess dbaccess;
private ResultSet rs;
private String strFindByAll = "select * from Dep";
private String strInsert = "insert into Dep values(?,?,?,?)";
private String strDelete = "delete from Dep where DepartmentId=?";
private String strUpdate = "update Dep set DepartmentId=?,DepartmentName=?,DepartmentUnit=?,DepartmentType=? where ID=?";
private String strFindByKey = "select * from Dep where ID=?";
public int insert(DepVo vo)
{
dbaccess = new DBAccess();
dbaccess.getConn();
con = dbaccess.connDB();
try
{
stmt = con.prepareStatement(strInsert);
stmt.setString(1, vo.getDepartmentId());
stmt.setString(2, vo.getDepartmentName());
stmt.setString(3, vo.getDepartmentUnit());
stmt.setString(4, vo.getDepartmentType());
stmt.executeUpdate();
return 1;
} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
return -1;
}
}
public int update(DepVo vo,int ID)
{
dbaccess =new DBAccess();
dbaccess.getConn();
con = dbaccess.connDB();
try
{
stmt = con.prepareStatement(strUpdate);
stmt.setString(1, vo.getDepartmentId());
stmt.setString(2, vo.getDepartmentName());
stmt.setString(3, vo.getDepartmentUnit());
stmt.setString(4, vo.getDepartmentType());
stmt.setInt(5, ID);
stmt.executeUpdate();
return 1;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return -1;
}
}
public List findByAll()
{
List list =new ArrayList();
dbaccess =new DBAccess();
dbaccess.getConn();
con =dbaccess.connDB();
DepVo vo;
try
{
stmt=con.prepareStatement(strFindByAll);
rs = stmt.executeQuery();
while(rs.next())
{
vo = new DepVo();
vo.setID(rs.getInt("ID"));
vo.setDepartmentId(rs.getString("DepartmentId"));
vo.setDepartmentName(rs.getString("DepartmentName"));
vo.setDepartmentUnit(rs.getString("DepartmentUnit"));
vo.setDepartmentType(rs.getString("DepartmentType"));
list.add(vo);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
public DepVo findByKey(int ID)
{
dbaccess =new DBAccess();
dbaccess.getConn();
con =dbaccess.connDB();
DepVo vo = null;
try {
stmt = con.prepareStatement(strFindByKey);
stmt.setInt(1, ID);
rs = stmt.executeQuery();
if(rs.next())
{
vo = new DepVo();
vo.setID(rs.getInt("ID"));
vo.setDepartmentId(rs.getString("DepartmentId"));
vo.setDepartmentName(rs.getString("DepartmentName"));
vo.setDepartmentUnit(rs.getString("DepartmentUnit"));
vo.setDepartmentType(rs.getString("DepartmentType"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally
{
dbaccess.closeConn(con);
dbaccess = null;
}
return vo;
}
// 删除
public int delete(DepVo vo)
{
dbaccess =new DBAccess();
dbaccess.getConn();
con =dbaccess.connDB();
try {
stmt=con.prepareStatement(strDelete);
stmt.setString(1,vo.getDepartmentId());
stmt.executeUpdate();
return 1;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return -1;
}
}
public static void main(String args[])
{
DepDao dao = new DepDao();
DepVo vo = new DepVo();
// 添加
// vo.setDepartmentId("1006");
// vo.setDepartmentName("科技部");
// vo.setDepartmentUnit("子路支行");
// vo.setDepartmentType("1");
// dao.insert(vo);
// 查询所有
// List list =dao.findByAll();
// for(int i=0;i<list.size();i++)
// {
// DepVo xvo =(DepVo)list.get(i);
// System.out.println(xvo.getDepartmentId());
// System.out.println(xvo.getDepartmentName());
// System.out.println(xvo.getDepartmentUnit());
// System.out.println(xvo.getDepartmentType());
// }
// 按编号来查询
// vo = dao.findByKey(3);
//
// System.out.println(vo.getDepartmentId());
// System.out.println(vo.getDepartmentName());
// System.out.println(vo.getDepartmentUnit());
// System.out.println(vo.getDepartmentType());
// 更新
vo.setDepartmentId("333");
vo.setDepartmentName("yewu部");
vo.setDepartmentUnit("jia");
vo.setDepartmentType("0");
dao.update(vo,14);
System.out.println(vo.getDepartmentId());
System.out.println(vo.getDepartmentName());
//
// 删除
// vo.setDepartmentId("1004");
// dao.delete(vo);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -