📄 deptaccess.java
字号:
/*
* DeptAccess.java
*
* Created on 2007年5月23日, 上午1:31
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package vincent;
import java.util.*;
import java.sql.*;
import plugin.*;
/**
*
* @author Virlene Cheng
*/
public class DeptAccess
{
private IDBResource dbRes;
/** Creates a new instance of DeptAccess */
public DeptAccess(IDBResource dbRes)
{
this.dbRes = dbRes;
}
public int insert(DeptInfo deptInfo)
{
try
{
Connection conn = dbRes.getConnection();
String sql = "INSERT INTO Dept(DeptId, DeptName, Description) " +
" VALUES(?, ?, ?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, deptInfo.getDeptId());
ps.setString(2, deptInfo.getDeptName());
ps.setString(3, deptInfo.getDescription());
int rs = ps.executeUpdate();
return rs;
}
catch (SQLException ex)
{
ex.printStackTrace();
return -1;
}
}
public int update(DeptInfo deptInfo)
{
try
{
Connection conn = dbRes.getConnection();
String sql = "UPDATE DeptInfo SET DeptName = ?, Description = ? WHERE DeptId = ?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, deptInfo.getDeptName());
ps.setString(2, deptInfo.getDescription());
ps.setString(3, deptInfo.getDeptId());
int rs = ps.executeUpdate();
return rs;
}
catch (SQLException ex)
{
ex.printStackTrace();
return -1;
}
}
public int delete(DeptInfo deptInfo)
{
try
{
Connection conn = dbRes.getConnection();
String sql = "DELETE FROM DeptInfo WHERE DeptId = ?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, deptInfo.getDeptId());
int rs = ps.executeUpdate();
return rs;
}
catch (SQLException ex)
{
ex.printStackTrace();
return -1;
}
}
public DeptInfo getDeptById(String deptId)
{
try
{
Connection conn = dbRes.getConnection();
String sql = "SELECT * FROM DeptInfo WHERE DeptId = ?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, deptId);
ResultSet rs = ps.executeQuery();
ArrayList<DeptInfo> depts = new ArrayList<DeptInfo>();
if(rs.next())
{
DeptInfo dept = new DeptInfo();
dept.setDeptId(rs.getString("DeptId"));
dept.setDeptName(rs.getString("DeptName"));
dept.setDescription(rs.getString("Description"));
return dept;
}
return null;
}
catch (SQLException ex)
{
ex.printStackTrace();
return null;
}
}
public ArrayList<DeptInfo> getAllDepts()
{
try
{
Connection conn = dbRes.getConnection();
String sql = "SELECT * FROM DeptInfo";
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
ArrayList<DeptInfo> depts = new ArrayList<DeptInfo>();
while(rs.next())
{
DeptInfo dept = new DeptInfo();
dept.setDeptId(rs.getString("DeptId"));
dept.setDeptName(rs.getString("DeptName"));
dept.setDescription(rs.getString("Description"));
depts.add(dept);
}
return depts;
}
catch (SQLException ex)
{
ex.printStackTrace();
return null;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -