📄 salesman.java
字号:
import java.sql.*;import java.util.*;import java.sql.Date;public class Salesman{ private String empid=""; private String idno=""; private String name=""; private byte gender; private String phone=""; private int deptid; 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 getSalesman(){ String select="Select * from salesman "+ "Order by empid"; ArrayList tablList=new ArrayList(); try{ PreparedStatement prepStmt=con.prepareStatement(select); ResultSet rs=prepStmt.executeQuery(); Salesman t=null; while(rs.next()){ t=new Salesman(); t.empid=rs.getString(1); t.idno=rs.getString(2); t.name=rs.getString(3); t.gender=rs.getByte(4); t.phone=rs.getString(5); t.deptid=rs.getInt(6); t.setConnection(); tablList.add(t); } prepStmt.close(); }catch (SQLException ex){ System.out.println("SQLException:"+ex.getMessage()); } return tablList; } public void loadSalesman(String empid) throws Exception{ String select ="select * from salesman "+"where empid=?"; try{ PreparedStatement prepSt = con.prepareStatement(select); prepSt.setString(1,empid); ResultSet rs = prepSt.executeQuery(); if(rs.next()){ this.empid=rs.getString(1); idno=rs.getString(2); name=rs.getString(3); gender=rs.getByte(4); phone=rs.getString(5); deptid=rs.getInt(6); prepSt.close(); }else{ prepSt.close(); throw new Exception(empid+" 已被删除或修改编号。"); } }catch(SQLException ex){ System.out.println("SQLException:"+ex.getMessage()); } } public void insertSalesman() throws Exception{ String insert = "insert into salesman values(?,?,?,?,?,?)"; try{ PreparedStatement prepSt = con.prepareStatement(insert); prepSt.setString(1,empid); prepSt.setString(2,idno); prepSt.setString(3,name); prepSt.setByte(4,gender); prepSt.setString(5,phone); prepSt.setInt(6,deptid); prepSt.executeUpdate(); }catch (SQLException ex){ throw new Exception(empid+" 不存在。"); } } public String getEmpid(){ return empid; } public void setEmpid(String empid){ this.empid=empid; } public String getIdno(){ return idno; } public void setIdno(String idno){ this.idno=idno; } public String getName(){ return name; } public void setName(String name){ this.name=name; } public byte getGender(){ return gender; } public void setGender(byte gender){ this.gender=gender; } public String getPhone(){ return phone; } public void setPhone(String phone){ this.phone=phone; } public int getDeptid(){ return deptid; } public void setDeptid(int deptid){ this.deptid=deptid; } public void updateSalesman()throws Exception{ String update="update salesman set idno=?,name=?,gender=?,phone=?,deptid=? where empid=?"; try{ PreparedStatement prepSt = con.prepareStatement(update); prepSt.setString(6,empid); prepSt.setString(1,idno); prepSt.setString(2,name); prepSt.setByte(3,gender); prepSt.setString(4,phone); prepSt.setInt(5,deptid); prepSt.executeUpdate(); prepSt.close(); }catch (SQLException ex){ System.out.println("SQLException:"+ex.getMessage()); throw new Exception("Update Fail:"+empid); } } public int deleteSalesman() throws Exception{ String delete="delete from salesman where empid=?"; try{ PreparedStatement prepSt = con.prepareStatement(delete); prepSt.setString(1,empid); prepSt.executeUpdate(); prepSt.close(); return 1; }catch (SQLException ex){ System.out.println("SQLException:"+ex.getMessage()); throw new Exception("Delete Fail:"+empid); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -