📄 department.java
字号:
import java.sql.*;import java.util.*;import java.sql.Date;public class Department{ private int deptid; private String deptname=""; private String headerid=""; private Connection con; public void setConnection()throws SQLException{ try{ Class.forName("com.sybase.jdbc.SybDriver"); } catch(ClassNotFoundException ce){ System.out.println("SQLException:"+ce.getMessage()); return; } con=DriverManager.getConnection("jdbc:sybase:Tds:localhost:2638/SALES?JCONNECT_VERSION=5","dba","sql"); } public ArrayList getDepartment(){ String select="Select * from department "+ "Order by deptid"; ArrayList tablList=new ArrayList(); try{ PreparedStatement prepStmt=con.prepareStatement(select); ResultSet rs=prepStmt.executeQuery(); Department t=null; while(rs.next()){ t=new Department(); t.deptid=rs.getInt(1); t.deptname=rs.getString(2); t.headerid=rs.getString(3); t.setConnection(); tablList.add(t); } prepStmt.close(); }catch (SQLException ex){ System.out.println("SQLException:"+ex.getMessage()); } return tablList; } public int loadDepartment(int deptid) throws Exception{ String select ="select * from department "+"where deptid = ? "; try{ PreparedStatement prepSt = con.prepareStatement(select); prepSt.setInt(1,deptid); ResultSet rs = prepSt.executeQuery(); if(rs.next()){ this.deptid=rs.getInt(1); deptname=rs.getString(2); headerid=rs.getString(3); prepSt.close(); }else{ prepSt.close(); throw new Exception(deptid+" 已被删除或修改编号。"); } }catch(SQLException ex){ System.out.println("SQLException:"+ex.getMessage()); } return deptid; } public int insertDepartment() throws Exception{ String insert = "insert into department values(?,?,?)"; try{ PreparedStatement prepSt = con.prepareStatement(insert); prepSt.setInt(1,deptid); prepSt.setString(2,deptname); prepSt.setString(3,headerid); prepSt.executeUpdate(); }catch (SQLException ex){ throw new Exception( deptid+" 不存在。"); } return deptid; } public int getDeptid(){ return deptid; } public void setDeptid(int deptid){ this.deptid=deptid; } public String getDeptname(){ return deptname; } public void setDeptname(String deptname){ this.deptname=deptname; } public String getHeaderid(){ return headerid; } public void setHeaderid(String headerid){ this.headerid=headerid; } public int updateDepartment()throws Exception{ String update="update department set deptname=?,headerid=? where deptid=?"; if(deptid==0) return 0; try{ PreparedStatement prepSt = con.prepareStatement(update); prepSt.setInt(3,deptid); prepSt.setString(1,deptname); prepSt.setString(2,headerid); prepSt.executeUpdate(); prepSt.close(); return deptid; }catch (SQLException ex){ System.out.println("SQLException:"+ex.getMessage()); throw new Exception("Update Fail:"+deptid); } } public int deleteDepartment() throws Exception{ String delete="delete from department where deptid=?"; try{ PreparedStatement prepSt = con.prepareStatement(delete); prepSt.setInt(1,deptid); prepSt.executeUpdate(); prepSt.close(); deptid=0; return 1; }catch (SQLException ex){ System.out.println("SQLException:"+ex.getMessage()); throw new Exception("Delete Fail:"+ deptid); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -