📄 customer.java
字号:
import java.sql.*;import java.util.*;import java.sql.Date;public class Customer{ private int custid; private String name=""; private String prov=""; private String city=""; private String phone=""; private String unit=""; 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 getCustomer(){ String select="Select * from customer "+ "Order by custid"; ArrayList tablList=new ArrayList(); try{ PreparedStatement prepStmt=con.prepareStatement(select); ResultSet rs=prepStmt.executeQuery(); Customer t=null; while(rs.next()){ t=new Customer(); t.custid=rs.getInt(1); t.name=rs.getString(2); t.prov=rs.getString(3); t.city=rs.getString(4); t.phone=rs.getString(5); t.unit=rs.getString(6); t.setConnection(); tablList.add(t); } prepStmt.close(); }catch (SQLException ex){ System.out.println("SQLException:"+ex.getMessage()); } return tablList; } public void loadCustomer(int custid) throws Exception{ String select ="select * from customer "+"where custid=?"; try{ PreparedStatement prepSt = con.prepareStatement(select); prepSt.setInt(1,custid); ResultSet rs = prepSt.executeQuery(); if(rs.next()){ this.custid=rs.getInt(1); name=rs.getString(2); prov=rs.getString(3); city=rs.getString(4); phone=rs.getString(5); unit=rs.getString(6); prepSt.close(); }else{ prepSt.close(); throw new Exception(custid+" 已被删除或修改编号。"); } }catch(SQLException ex){ System.out.println("SQLException:"+ex.getMessage()); } } public void insertCustomer() throws Exception{ String insert = "insert into customer values(?,?,?,?,?,?)"; try{ PreparedStatement prepSt = con.prepareStatement(insert); prepSt.setInt(1,custid); prepSt.setString(2,name); prepSt.setString(3,prov); prepSt.setString(4,city); prepSt.setString(5,phone); prepSt.setString(6,unit); prepSt.executeUpdate(); }catch (SQLException ex){ throw new Exception(custid+" 不存在。"); } } public int getCustid(){ return custid; } public void setCustid(int custid){ this.custid=custid; } public String getName(){ return name; } public void setName(String name){ this.name=name; } public String getProv(){ return prov; } public void setProv(String prov){ this.prov=prov; } public String getCity(){ return city; } public void setCity(String city){ this.city=city; } public String getPhone(){ return phone; } public void setPhone(String phone){ this.phone=phone; } public String getUnit(){ return unit; } public void setUnit(String unit){ this.unit=unit; } public void updateCustomer()throws Exception{ String update="update customer set name=?,prov=?,city=?,phone=?,unit=? where custid=?"; try{ PreparedStatement prepSt = con.prepareStatement(update); prepSt.setInt(6,custid); prepSt.setString(1,name); prepSt.setString(2,prov); prepSt.setString(3,city); prepSt.setString(4,phone); prepSt.setString(5,unit); prepSt.executeUpdate(); prepSt.close(); }catch (SQLException ex){ System.out.println("SQLException:"+ex.getMessage()); throw new Exception("Update Fail:"+custid); } } public int deleteCustomer() throws Exception{ String delete="delete from customer where custid=?"; try{ PreparedStatement prepSt = con.prepareStatement(delete); prepSt.setInt(1,custid); prepSt.executeUpdate(); prepSt.close(); return 1; }catch (SQLException ex){ System.out.println("SQLException:"+ex.getMessage()); throw new Exception("Delete Fail:"+custid); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -