该用户从未签到
|
- package s2.jsp.zhangxiao.entity;
0 Z2 F# Q$ ~6 i0 J5 a! p! |% V - /**/ Z0 i- @$ j4 q1 a1 G+ ]
- * 物资实体类
, R3 M8 \, C/ }3 y - * @author Administrator
( A6 @0 j q6 \0 h* e# X - *4 f' J: {* L1 X- f$ s" y* j
- */
4 B3 k7 W B W- n5 ` - public class Materials {. y+ _+ q6 _" a. h
- // id, name, spec, number, price, producer/ a! g( J h# a
- private int id;$ {5 q( U# w8 F! C0 e
- private String name;
; K6 d6 p0 y" o) K+ ]' n# T- U/ M - private String spec;* d* n% K. S4 f" O, F9 G
- private int number;/ o3 V% m0 V. _: D f" b; e
- private int price;
# H! N8 U# u; ^! z9 d& x4 V - private String producer;
" l. x X* b; v- G - /**8 v/ M+ C' Q% t
- * 有参构造函数
, o' ] ?' y$ \ - * @param id; U" u% m' r1 Q( F1 [
- * @param name
! \( t" T5 F5 r& E2 G - * @param spec
6 r& _* C$ C5 R - * @param number
5 L$ d9 ^! H2 y: _. i+ o9 c2 y2 F& Y - * @param price
1 l4 K* e' L0 b2 K- ` N - * @param producer
+ r8 ~, |, ?3 F* ~9 z6 t - */! n* j% A ~' `, b& _
- public Materials(int id, String name,String spec ,int number,int price,
1 B) x9 U6 J2 a+ \& ` - String producer ) {& J0 q1 Y5 w+ p; o7 `) F- l; V
- super();
) M) v2 D) D3 w H# f - this.id = id;5 g+ @3 y! t/ a& C8 s' R
- this.name = name;
1 W# w8 d& [2 x - this.number = number;
; S% L9 F; y4 Y. F6 A# g e D - this.price = price;: v/ Y t1 \ A+ z; E
- this.producer = producer;
% h/ b6 t2 ^. w) l6 F& L - this.spec = spec;
1 |- {3 T" ~/ l' ]. o5 A$ e' u! s - }
9 u1 m' j9 l7 h/ x! y% J - /**% h# |/ B6 \8 ^/ ?) W: R: t% ]/ q4 V( x
- * 无参构造函数, a+ x; t/ C4 |
- */
4 f. _% i. c) o0 ~- F& n" Z - public Materials() {: a0 o* t7 N& q/ v( D9 \
- super();
! T2 w A) N3 E. F - }
) l* e! E% }+ j. l8 Q5 t - /**
* b$ t. S% Y! d1 L9 l( u3 g - * 编号
5 h7 a% q& s8 B2 H* K+ _6 B - * @return
- Y* J0 i6 Q( ]# S8 O% p+ i2 L/ s) D - */
4 e) c: N' N. X4 a - public int getId() {
6 W% D: w5 l& S( S, h - return id;
( c, ^2 @& v' x6 ]. q7 V! ? - }
0 S4 P+ v6 m5 p; ]" o- I+ }8 _ - /**
! A) a& F; r8 o& `, _ - * 编号0 w3 z* B7 _1 c' y1 {& g
- * @return
, \8 w# u. T+ \% k) v# H6 m - */+ Y9 h u. B, K& o. g. C
- public void setId(int id) {
# B: P7 d1 I0 n7 P2 K - this.id = id;
0 ]$ e6 U" |4 b7 Z" C: [0 L - }) L1 E6 \/ g% P) P& B3 L
- /** \2 ?4 ]& E" l# w' \/ K8 n
- * 物资名称% L8 F1 N9 l* u% L2 F3 h1 y
- * @return0 J# t7 h6 U; ~* y U* o
- */ Y. Q7 G. `9 o% H% x
- public String getName() {3 j7 t, ]; b' T+ R
- return name;
3 b# N$ Q) h7 B - }$ E& a0 } K# i6 a7 ^0 n
- /**' J3 t2 e' |, W# z! w: c
- * 物资名称
/ G2 F) N5 l+ P/ B; I - * @return
' [% \2 X! I5 [% r( O4 r( k& ^ - */
, n; L; f! n0 m$ ^6 X' x - public void setName(String name) {
! j+ q: }' \3 c2 T9 H8 p" N9 k - this.name = name;
0 O; Y" I3 ~) S( }/ i0 U. \ - }
m: I5 Y, K2 c$ v: ?( z) D - /**
' R8 R' a" K! Q* l% U5 z( @ - * 物资规格& F" j& K$ Z4 \8 p# `) U
- * @return
* _0 b* Q( f* _) V1 j' N% C) G9 c( R - */
. q5 O0 _. I; O9 }1 P% U - public String getSpec() {$ Y& E3 y. q* c( \0 c0 e. m4 [
- return spec;7 I% ?& k$ E6 [, C2 e
- }
8 Z# s ^$ [0 l d - /**
' T7 y: K$ t* z6 f! _, c - * 物资规格& i7 N& `* e1 N: i$ o! T
- * @return2 k/ P% s( ]8 {
- */
$ \4 Z* d0 ^; \ - public void setSpec(String spec) {6 t/ [, L0 E& S3 q7 v3 F$ ^! ]
- this.spec = spec;
# J7 s+ F, w" q" d - }2 Z Y8 h: N% i
- /** Z) b% j. g2 ^, R. l. N
- * 物资数量1 i: a0 Z- `, C# I* E7 Y5 g5 X& l7 U
- * @return/ t# R- [. @8 I9 T1 ^# b
- */
$ m3 f3 k! a% R/ u. h - public int getNumber() {
" @, E2 G( ^7 p% W( O3 W - return number;+ O& w5 J X3 J! u4 x' _. G0 I% a
- }
5 Z3 U- \ E! M& ~ - /**
- G6 T# F7 \) y6 n/ u: z - * 物资数量! H6 ]% I5 G" O$ d, J, H- }
- * @return
, u8 |# U! t) L: E0 J) a+ J - */
" I7 s/ u8 v% Z1 f6 O4 F/ ^8 K4 z9 U - public void setNumber(int number) {7 B. r* I+ J! D7 _4 y/ V+ N# s) r
- this.number = number;( k, L* I$ k" z; ]' k
- }
J. O% H1 B, b+ T* A8 t {0 B - /**
' Q6 j5 c s4 z - * 物资单价
& t( Z7 k& I H. j# K2 s! h, W - * @return. |4 q+ P' g+ D% T
- */
+ t6 s2 Q) N: t& i% d& R - public int getPrice() {" Q% e1 D9 h X9 |' N: W# j2 m
- return price;
: o$ o- F' h" ]* P# { - }1 [2 [) k9 p# \1 {. d3 \% H d: b
- /**. f6 e6 {2 R! d: P: W% s
- * 物资单价- W# z! A* u: ?& l1 N9 z
- * @return
; ?3 \& z+ y2 T! y/ ^ - */; \2 @' x% n/ i! \
- public void setPrice(int price) {3 @- R/ I! Q2 O2 w- n2 v/ G# |9 W
- this.price = price;3 i3 j' o2 n1 X# j; N
- }
O5 F) Y* j5 S- d - /**
' _' Q4 w1 Q6 ] - * 生产厂商0 Z* m9 \+ r; G
- * @return
/ K Q* q( v; \$ l6 N4 D6 M - */7 I* S( c- e) V# W% J2 w
- public String getProducer() {4 z# h) C' |# C3 `* c4 X
- return producer;
e5 u; [) v7 v- h0 {# o - }
1 P" n- ]% C7 a4 } - /**( o _; u' \# o; h8 y/ V2 e
- * 生产厂商
! Y/ }, T. N6 h3 `/ X. M - * @return8 L2 }( e/ d+ r1 [) S6 F& H
- */
2 s$ Z2 W: _, Q0 d; l7 {+ m+ U - public void setProducer(String producer) {
+ ^; {; J6 a! _0 w" Z3 U+ t - this.producer = producer;
; m3 {2 c8 c5 I; D7 r8 g - }
$ m# ^! ~3 K/ G0 G2 I! m -
% @7 [: h) u. D - }" l. O( |. j" Q2 [# q
复制代码- package s2.jsp.zhangxiao.daoImpl;2 F/ j" M: U2 L8 s B
- import java.sql.PreparedStatement;. `! G9 u" ?, V H2 j, X8 o
- import java.sql.ResultSet;
+ K! @( R' d! w* ~" ` - import java.sql.Connection;
6 e+ S& q5 E; b$ l% J - import java.sql.SQLException;
) f: b: X3 G2 M$ @# l, d: E - import java.util.ArrayList;
- Q, C: @* J& I; U4 {0 e! B3 @ - import java.util.List;( {; y' U" `7 g5 Y% n, b
- b4 B& V2 \9 t5 n4 b' ^- import s2.jsp.zhangxiao.dao.MaterialsBase;0 d8 I* W/ v" e6 g u3 b0 z2 q- }
- import s2.jsp.zhangxiao.dao.MaterialsDao;
' d8 N/ U& w( b0 N; ?1 U- Z+ z - import s2.jsp.zhangxiao.entity.Materials;
" L# c# l& u Z - /**; S0 J' ~- A( J1 Q2 E' x' E
- * 实现接口的方法# J8 T* ]7 A" m6 g, u
- * @author Administrator
1 x5 _, k8 H: s8 O/ l3 J2 b - *
, Q. g. G/ A5 ~9 X B& x! B& T" w - */
4 t! Z" {. |* @! R } - public class MaterDaoImpl implements MaterialsDao {( D8 q4 l! r1 u. {6 H+ P
- /**3 Q/ s0 e5 P( g. R3 U
- * 查询全部
) j. [1 x, L0 k$ j* Q# m - */
& i4 o, T$ j# K. l- q4 ] - public List materialsAll() {
- K1 o/ a% W: a; b7 r5 ]& R - List list=new ArrayList();1 `( H, T+ y, w, }. S P) R
- Connection con=null;
" e* g$ h! ]. l7 h$ Y8 S - PreparedStatement past=null;
; i& Q/ W8 {- t$ V& A* I/ P - ResultSet rs=null;+ ^2 m; w3 E+ y' J. {
- //sql 语句
e! \; \( v2 v# d, K1 Z. j8 k, E - String sql="select * from materials";
0 \; a/ U/ |# u+ m4 j$ K - //连接数据库
% }6 L9 S7 `+ N' ?- w! w: c' ]/ L+ Z - con=MaterialsBase.GetConnection();2 D; ^; _& D2 H4 q, \: M* `" c
- Materials materials=new Materials();
" U, e: R, z& q. C - try {
2 [% [/ b/ v; d3 b' N - //传入sql 4 H8 `: |3 O( h' W9 o
- past=con.prepareStatement(sql);* ]6 A( w; n+ L
- //执行方法
: i* v+ W& j0 M) r! {2 X1 j! Z" f - rs=past.executeQuery();% R7 ~8 S( I* h8 C" J
- //循环读取数据
2 G2 p- \. Z: m - while(rs.next()){4 b+ l; l: H+ i% r2 W9 d; `
- // id, name, spec, number, price, producer
& R1 M. y) X3 {3 G5 m - //编号
2 C$ \! \6 S: y( [; A - // int id=rs.getInt("id");
5 @: B$ V- [ F8 l/ N2 _ - // //名称/ Y* s4 |4 o$ }6 A: z7 b0 Y
- // String name=rs.getString("name");: n/ M3 Q) @) o) l5 u
- // //规格
# j; R9 j1 T. C& m+ h# l - // String spec=rs.getString("spec");' N$ d! j9 \0 n4 X* m- p
- // //数量
% d" {# R3 ]+ ]) `) R+ |/ ?, K: M - // int number=rs.getInt("number");/ O+ L. Z) q8 d8 K& w6 J2 L6 ]1 z
- // //单价
, r2 L) @* ?* ^* L - // int price=rs.getInt("price");
" q0 n H; }. I5 k, ^; [ - // //生产厂商, d! p" P& r" K0 o) M6 q2 S7 ^
- // String producer=rs.getString("producer");
1 S1 j: n$ j: a0 U - // //添加到构造函数& B) Z' Z2 ~/ w& A/ C
- // Materials materials=new Materials(id, name, spec, number, price, producer);
! C) a! j5 L( n! \ - // //添加到list 集合$ ]- F' j: d4 U& O3 m; J+ N$ P
- materials.setId(rs.getInt("id"));7 ^3 _0 z) q4 Q/ ~' p
- materials.setName(rs.getString("name"));
, i4 n0 K3 H- g3 a+ | - materials.setPrice(rs.getInt("price"));
# O1 J0 e T3 F7 [. `* o - materials.setNumber(rs.getInt("number"));
3 B! @ u1 }2 ?' H3 D! g& m e - materials.setSpec(rs.getString("spec"));
8 b% I! D, x- |% ] - list.add(materials);
9 A: A- p4 ^2 C& N: B - }
( R# O( d8 K- @2 _5 T0 G) C* p1 q - } catch (SQLException e) {, ^" ^4 ^ V- B: z+ ]5 b
- System.out.println("materialsAll 方法出现异常");6 I! c0 ~' F+ M# v5 r* t: a8 ~
- e.printStackTrace();
2 L# Z; n' J: R$ x2 G - }
6 Z! O* r. c3 |# N -
4 h" i: d ?+ S7 Z - return list;* B# c0 F' T% o$ R6 H" r. {: P
- }6 C5 n8 P2 u. b+ A- _8 w+ D6 G) W# L
-
1 ?7 ?9 f& A/ J- f - /**
. E- k5 g( J8 a C; |8 B5 i7 @* } w; p* b - * 根据id 查找物资信息8 J( [' j H' h) v9 M
- */
; P" l* u1 l, w+ \! h - public Materials materialsID(int id) {
% q/ N$ }2 c$ M, R8 d1 C0 z - //声明物资类9 _" \' P4 O8 V+ t& H7 a+ F) j
- Materials materials=null;7 J" h; Z1 I1 [ U, G
- Connection con=null;
1 s, D ~. h* R* C# i: n/ |4 ` - PreparedStatement past=null;9 J- D; o- h, u/ W
- ResultSet rs=null;: H- w( n' V( B( d
- //sql 语句& J" q- b1 F) q) \+ M
- String sql="select * from materials where id="+id;8 a& S( L1 M5 e( W
- //数据库 连接
& m% l4 m% m- } - con=MaterialsBase.GetConnection();- s$ C$ _7 Q) I& o$ @
- try {1 Y! r+ @7 l# r+ U; i+ j; z* c
- //传入sql
U/ W* D) J8 l4 Y - past=con.prepareStatement(sql);0 j4 }% D, t) w: g0 y5 w# c
- //执行方法/
# h1 J: k" R$ E" T - rs=past.executeQuery();$ G4 p! y ]& Y, G( u! Y0 C
- materials=new Materials();
- `2 i* t, G% _8 F6 {0 B - //读取数据: _% b0 g# O. I% Y' a
- if (rs.next()) {9 G% O" f( A7 R7 X! ?& b
- //名称5 i3 w/ \" l6 S1 s* W$ e
- materials.setName(rs.getString("name"));3 n" e0 `2 T6 E8 F: J- _7 }
- //规格
# \9 K6 n6 n1 { - materials.setSpec(rs.getString("spec"));* S4 k! r$ Y6 I# \4 M
- //数量
( {9 A3 q& Z! { - materials.setNumber(rs.getInt("number"));
9 s1 ]( J; g2 U( d! }1 I - //单价5 y) O) e# W4 l0 ?7 R
- materials.setPrice(rs.getInt("price"));
) d, z1 R' U5 M5 f" f5 X% u1 k - //生产厂商$ J- ~! }3 ^3 i9 n; U g
- materials.setProducer(rs.getString("producer"));; g4 }2 n! ]4 w" E- Q6 j* K3 p
- }
% O+ G8 [, S# }8 b1 }- E0 c0 ?2 e+ ` - } catch (SQLException e) {
! [- o1 G& P S: G# _7 V - System.out.println("materialsID 方法异常");
6 _9 e& H: H. G/ Q9 }) {* q - e.printStackTrace();6 z/ n+ d P" t. B" x
- }finally0 n3 ~/ L6 D* x! c; s J* c
- {
6 b0 I7 i( T* m1 y ? - MaterialsBase.CloseAll(con, past, rs);1 f' D& v0 Q8 X" i0 d# z( J" {$ H8 L$ L
- }
2 G. k2 `; w1 |- b5 c! s# M. O; \ - return materials;6 }& n0 c, @) f
- }/ M2 D: {4 E5 T
- /**
6 l9 x1 h! }9 E) W6 c - * 更新物资信息
# n3 Y0 _7 v" Y$ v" k, @& ^ - */
% b- w k; _. U# s5 y- o; v - public int materialsUpdate(Materials materid) {
8 R" T. Z* y) e% e- B0 N+ D - //sql 语句8 f, L( C9 N( V
- String sql="update materials set number=?,price=? where id=?";3 f' \7 H9 e/ X) F6 ]: h) Y5 h
- //
. }; _; q- T' e" s$ n - String[]getValues={materid.getNumber()+"",materid.getPrice()+"",materid.getId()+""};" K9 U# U4 R* c# q! y
- return MaterialsBase.ExecutUpdate(sql, getValues);
5 `1 M5 _4 ], z: e0 y - }2 p/ W3 E; N+ E& S. ~4 d" r7 g
- 0 g) e* |5 ]" e7 x
- }5 D# R. D! [. S, L
复制代码 ( k( Z$ }5 G* |" w
代码案例下载:点击下载
9 _2 G" O8 {) q7 @2 ^$ G H p
5 T2 | N$ |6 I/ h1 A1 e |
|