该用户从未签到
|
- package s2.jsp.zhangxiao.entity;* e4 \* [# {: r
- /**
5 e& t- _' g+ {* A1 e - * 物资实体类9 l0 X* s- o- S6 p3 e6 r: o
- * @author Administrator5 V, H. B! R% r5 _5 F6 @: e' g% P
- *6 K( }2 }2 G' x
- */
; T; m2 ?/ q' C: m( e, W0 K p - public class Materials {
+ c+ d) G5 V+ O6 N+ [9 m7 k( I - // id, name, spec, number, price, producer3 g' [; ~# W; y
- private int id;& f' q0 P$ Z H" k' s: }
- private String name;
, y% G5 V S6 k3 G1 l - private String spec;
+ w* d) ]: K6 R; l6 e4 Y, z - private int number;/ m0 Y i/ ~1 h1 b
- private int price;
" H% c: L X3 ]- p3 f - private String producer;
! n2 I$ m8 D5 X2 t - /**5 M6 L/ L( H1 L% c0 G# G, \
- * 有参构造函数. {4 z. J" u! Y p @2 N7 X+ z
- * @param id
9 k- V, W# S! O$ p - * @param name
0 Q! E; ?' ^6 T7 t0 S L$ l - * @param spec8 [: Q# [& q. C" k
- * @param number! A6 [4 f6 N* q; w7 p! N: A
- * @param price
" T. l0 v: T( k! e - * @param producer
' Z& }, h% d+ I5 a4 C% V - */
8 e6 c. H6 j" Y g) c - public Materials(int id, String name,String spec ,int number,int price,
4 g$ B7 F. @/ s: W( _ - String producer ) {0 s7 |7 n3 Z* F! D3 S8 P$ {% F
- super();" f. c. C; i# Y1 e2 U# H- d
- this.id = id;) j$ s: P* w* y3 a: q
- this.name = name;' P3 b, x$ r1 m" i/ e# M# B& N9 D
- this.number = number;
9 x; ]2 j( Q4 v2 }8 H - this.price = price;/ t7 B* r0 j6 \, u
- this.producer = producer;4 J, e) y3 U* T* W
- this.spec = spec;: D. g/ C7 l1 j; T* n
- }
1 Q" U5 l5 f( r. n8 b8 m; N - /**
( k% o/ S: \' Q# x7 j5 Z& E9 _ - * 无参构造函数
5 l; }% K O# B8 ~5 h3 B, J - */* d* ~+ \: m' j
- public Materials() {
/ V# J! \" E. w5 ~9 _9 w - super();
0 ]: b9 R1 q/ \2 `, C* q r - }6 P! V0 G" W8 Y0 `& c
- /**- E4 w% U+ h& `% u+ r
- * 编号6 B3 I6 S& ]3 M- ?" \- X: n ]
- * @return( L9 k% R" i7 ?, u+ R$ ]2 h
- */. c2 Q0 W# t' f
- public int getId() {/ ]5 ^0 _& F" i6 b! a8 F
- return id;9 }, {# ?; N$ x$ X) ?
- }: `. Q# }' w" I0 `1 B
- /**
$ w f3 W; a+ u( P* G. } - * 编号- x0 p% c6 h; P+ F/ D6 c! b
- * @return
. R* y& ~8 Q' k E9 _& M1 c - */. ~1 W6 o% M) d6 u" z
- public void setId(int id) {2 \, q W7 @( ~7 e o
- this.id = id;
. N- O7 I0 c* v" q* ^! F - }2 N& @& }$ ~( C) v
- /**
6 L" }* V1 O! x1 B - * 物资名称7 n4 q5 r" y5 ^$ D/ \) U% s. D
- * @return1 {6 E! n: f4 {7 R
- */) O/ D/ x2 G, M, m+ I% |
- public String getName() {
" G# Z$ |- |9 o2 W9 b - return name;
1 [9 c, t- x/ \) q5 t& A6 E - }
7 O f5 ^0 \3 }- m$ L/ z3 i - /**
6 r, ]# L# }- C, X. V9 T x8 o - * 物资名称
$ I9 O. E$ ~* P* D( d: l: o - * @return; r8 R0 @& B3 \* U8 X. p4 p+ l
- */+ O. e2 E7 ] l& n/ z8 ]" C
- public void setName(String name) {& l5 k5 c; O0 V' j$ F0 d
- this.name = name;7 Z1 c8 z5 H5 c9 `
- }
3 y8 q. `# r* S: d! w7 r" o - /**& \( A/ `& L, K9 U) r2 a- t
- * 物资规格
% i% f0 F# g" A m$ T - * @return
4 D! u9 o6 s1 K5 a, r1 _- S4 f - */
. {/ l* R3 f% d f8 V- ] - public String getSpec() {
. k2 R! U: s. N3 D! Z' C! Q" L - return spec;
% h$ T" A+ b# r" t8 ]/ N7 c - }
( o, ?! v& `; m" s- v5 A a - /**
0 l3 Q+ _0 Z$ C1 _, y0 P - * 物资规格; H" B9 X, k, S' G8 x P( ^
- * @return& Z* I4 A2 k1 j% q4 M
- */
" B1 W5 e; R# v! }* c - public void setSpec(String spec) {
' [) z0 {# N* o6 @# V3 U5 _ - this.spec = spec;
0 H% I3 F# Y' U, } \& R. X6 L - }, Y) ]; ]$ ^8 P# U: a- b
- /**
; E# c1 @1 M8 h/ @; L/ I - * 物资数量
; }0 m7 B) M# U# v# o' u - * @return
p3 T( T# Y; } - */$ j; Q/ o% g) a Z- P
- public int getNumber() {* p% D% U4 ?* V' k
- return number;
0 h1 D: C$ N- o6 v6 [4 m4 ^ - }+ P4 B0 e- b& C, N
- /**
' M! ~& F( @- R3 V - * 物资数量* w/ U/ H0 D' u
- * @return9 C j) a1 g2 X# U( w
- */
0 n4 d- q0 A+ [" }1 |! E! r - public void setNumber(int number) {
) {& }: h" r( ^) x& l ^/ V$ K - this.number = number;
0 ?4 B& f" k" H5 J, f! j7 }% t - }" s8 P/ y' ?- \
- /**
* T* l! }: c0 M( F' M3 V2 N - * 物资单价
; z3 N) x0 R- T- B9 a' e5 {% h G - * @return
- L/ z3 I5 z3 J - */3 B& u1 i4 f, N* `
- public int getPrice() { M0 f7 k9 ?2 k
- return price;5 `7 F( j% \) d) m2 I0 J& o) V' [
- }# B- j1 t# g. H _4 r* k7 M
- /**
" s* l' |& {# ]7 d4 P - * 物资单价( @8 ]5 d$ N! ~3 H6 w: f
- * @return
, ^% e* B% o, G9 I: P* a - */
3 c* `1 M! K+ s) P. F3 \ - public void setPrice(int price) {4 N& ~. B, p2 c: b" o' A
- this.price = price;
* [8 q/ E- n/ d' X9 B* c - }
: [% ~6 h% D+ |- W9 }' A# ? - /**
, v: }7 N7 P' H& ` - * 生产厂商( [- n) `( N( Q9 y5 a1 O6 J7 ]
- * @return1 M: X! q2 U& I
- */
% i% E+ k6 n* a+ }0 K3 E - public String getProducer() {
6 U4 H7 n6 y+ v, [ - return producer;
0 U5 k4 T' _2 x$ ~$ w - }
$ K' s w" A% i0 K1 s - /**9 f* k) d2 l. U
- * 生产厂商( h R9 _5 ^- @6 u# [. X) D
- * @return/ X3 s: Q8 f8 h4 O6 D! M
- */
' G; W4 C! i- x5 X/ ~ - public void setProducer(String producer) {
4 z& u% ~* E' V( k% u$ { - this.producer = producer;
2 x; P" D8 {# W0 Y+ i$ B V - }4 }0 A; _9 V1 T7 b+ W
-
' M9 G |( y2 _: g, S P+ I - }
3 D3 A1 h {6 t' t
复制代码- package s2.jsp.zhangxiao.daoImpl;1 A* i( C% p8 D$ C. s. x+ K
- import java.sql.PreparedStatement;
6 M) ]& Q7 ~; I t6 G# J% q - import java.sql.ResultSet;
' o m# L+ n1 N. } n - import java.sql.Connection;
, e. j3 j* X' s$ N - import java.sql.SQLException;
" y k4 A& K/ t2 n4 D7 a, E% E - import java.util.ArrayList;
3 D0 Y6 y9 t: h. s- k X' { - import java.util.List;
4 }- o; Z" p6 @1 R+ z+ H! I% Z; c
' ], w6 [& |* C* A `- import s2.jsp.zhangxiao.dao.MaterialsBase;/ G# c7 p C# @. o Z
- import s2.jsp.zhangxiao.dao.MaterialsDao;/ }/ |2 f2 b5 p* K0 s9 f& w. B
- import s2.jsp.zhangxiao.entity.Materials;
% F; f; G8 R3 [: f - /**
- T! O4 Q" O$ Q4 j: T, P' b - * 实现接口的方法
* b) s u$ f$ J$ P( [. { - * @author Administrator
* \! h( C' t* r2 W% d - *
) m) o6 l5 I1 A4 n - */
9 Y& d& A. ?# u6 c5 q& C! ] - public class MaterDaoImpl implements MaterialsDao {
3 q6 X8 b. B$ b8 F - /**2 Z( a5 O* {# L
- * 查询全部4 [3 W+ _1 m w ~8 Y7 o& }
- */
+ i, H1 k( j7 S7 L/ `; g - public List materialsAll() {
) y6 ]5 J5 l7 k! U9 u5 q - List list=new ArrayList();
4 s9 N! L# E \. ?; b - Connection con=null;) m7 |. J% R5 ?5 i0 {9 N
- PreparedStatement past=null;
3 H' f2 E) k5 X - ResultSet rs=null;0 h- n2 F. F$ ?0 B
- //sql 语句" x9 y1 Q6 W, n/ a9 a; g
- String sql="select * from materials";/ v! N! E9 U. s( T+ y6 z$ b
- //连接数据库5 i, ?9 f4 j, T- d+ k! ^! f$ M5 b3 F
- con=MaterialsBase.GetConnection();
. J& {* c Q% X; _6 T6 e - Materials materials=new Materials();
" z# P) }0 u: i5 H1 Q3 x( U2 s8 q - try {; x" m$ C/ B0 g
- //传入sql
2 ?: n5 U% c3 x: R, q4 W - past=con.prepareStatement(sql);
) b- M- }2 |5 R; M# [3 F - //执行方法
0 V5 U0 q! Z- k6 i$ [ - rs=past.executeQuery();
3 H u' {: w" f# Y7 C3 f - //循环读取数据
, [( }& Y: [9 C, `. _- z - while(rs.next()){ U h5 T0 b5 t3 }( c6 i' D
- // id, name, spec, number, price, producer8 |# w3 n$ m6 L+ V0 w) f
- //编号
) Q, f* M! I' m2 ~4 o4 ]# s: ^1 c - // int id=rs.getInt("id");" o0 r& ]6 X5 g* t ]% ]9 p
- // //名称
: j3 m3 \$ h- _/ j" P2 L5 f6 b+ f - // String name=rs.getString("name");
+ \/ I& d& I" Z - // //规格# D4 c% z' L9 g4 s
- // String spec=rs.getString("spec");% w. K" b1 e6 m& ?$ q6 C! ~1 d
- // //数量
5 Q4 ` U1 w/ W ^7 K5 P - // int number=rs.getInt("number");
5 c% K3 @& T2 O3 B8 q" n+ Y3 L; s - // //单价: U, D) G$ l' O- W# f' A; v% t6 S
- // int price=rs.getInt("price");( p) G3 K/ @& o# ^& f+ ?
- // //生产厂商" g8 o8 j' {4 Y' G, }- g
- // String producer=rs.getString("producer");
# n8 O2 u& f7 R2 h4 Y3 Y. ~/ g - // //添加到构造函数
: b' ~; `3 H3 A - // Materials materials=new Materials(id, name, spec, number, price, producer);
1 C! P1 Q/ {8 k! S: {% f, Y; `# [ - // //添加到list 集合
2 m+ R5 E- x2 L: l - materials.setId(rs.getInt("id"));
0 s) [9 N; k0 d, W ~' H1 K1 Q5 w9 i - materials.setName(rs.getString("name"));! F( y! x( ?% g/ D0 R
- materials.setPrice(rs.getInt("price"));
6 s. p! [/ A$ Z6 ]$ {7 q3 H - materials.setNumber(rs.getInt("number"));" \0 o( n, D+ c, U" h
- materials.setSpec(rs.getString("spec"));7 r, U5 r3 _9 F% |
- list.add(materials);$ E* D) i% u) [4 f) Q5 P7 Z$ K; Z
- } 3 V: m9 D0 r7 R% M
- } catch (SQLException e) {
0 D; }3 [+ Y- B4 r' R' Q" y0 t - System.out.println("materialsAll 方法出现异常");, @. z( @ A( t) m ?1 ~% i
- e.printStackTrace();
1 T! b; V* D* l# e* W* v - }
* ]3 D w( b' t4 T - 3 ]$ O/ S6 b' ]
- return list;1 K+ M* K6 L% C _& p [4 d
- }
0 v5 b. |6 l: m, x) B1 z -
5 ^% a9 i7 R& {; N& p4 O& h - /**
4 Z: V- i0 A( m, d" ?$ [$ E7 Z - * 根据id 查找物资信息# \" g; q5 j! Q6 G; b3 ?: ^
- */
; Z2 ~/ |6 W0 [$ k' S6 H8 W - public Materials materialsID(int id) {
4 n0 C( E7 g8 V - //声明物资类3 t: l4 E! w# J/ ~# [
- Materials materials=null;
" h* p- a+ R& [- ?: u# K - Connection con=null;+ |% }! w, S3 a Q) m: ?& H
- PreparedStatement past=null;; q! O7 |! T7 W7 V! }
- ResultSet rs=null;+ o" t6 |' [, U& U1 G
- //sql 语句1 T5 P" V" y1 z: H# U
- String sql="select * from materials where id="+id; L4 a. E% G8 Y W2 d
- //数据库 连接3 b4 s9 s% f+ t5 k
- con=MaterialsBase.GetConnection();
7 {9 Q% f2 ]4 F& h# n1 w - try {. j q& t$ U( a" [+ q: T4 c( {
- //传入sql
8 r2 A3 Z. b/ U - past=con.prepareStatement(sql);7 x% y- C! y: b7 g: x, A
- //执行方法/
1 Y" r1 R! k! T$ ]' K3 }0 x, J$ P - rs=past.executeQuery();& e$ ~- q: b1 ^0 {
- materials=new Materials(); f( @3 [. l* s/ I( }; Z
- //读取数据 }. }$ b* c) v2 v6 k F/ |5 Y6 m
- if (rs.next()) {
, y4 Z9 v, W, A' X+ U8 D4 K3 q - //名称8 c( V+ n' A2 y/ I5 T& D& t
- materials.setName(rs.getString("name"));; ^: l1 [- e' n# O k0 u
- //规格0 g+ q7 x/ g. P/ K7 Q+ q, p
- materials.setSpec(rs.getString("spec"));! r J$ o5 @. H+ o4 B' T( b
- //数量
5 V- K" H! m" K: l% J9 Y' _4 l - materials.setNumber(rs.getInt("number"));
3 `) E/ j: V* [$ p3 o1 I - //单价
1 o2 u% B, s! H3 K - materials.setPrice(rs.getInt("price"));
& C/ w6 \9 q. L) i8 { - //生产厂商5 P& z- P# _5 W7 }# C6 j2 e+ ?3 l
- materials.setProducer(rs.getString("producer"));% h5 {" Z* l0 V
- }: d2 T5 ^% C6 Z( t
- } catch (SQLException e) {
$ S8 Z: D) z+ V$ W; X - System.out.println("materialsID 方法异常");
* N: T- F, z/ ]6 }7 P: a - e.printStackTrace();
0 b4 v8 [' N9 e3 v' C - }finally
! h1 j0 y$ a O) u# o - {
9 U4 Q4 S. g5 N7 T/ i - MaterialsBase.CloseAll(con, past, rs);
4 q& z( ?0 P2 D - }
, S' w' b, t( N/ r - return materials;
" w* H% c/ P$ v4 y7 b9 w - }
; ~- B7 p1 {- N9 H9 b0 n - /**
: z; r2 U2 T6 B! Q5 P7 K - * 更新物资信息
+ @# |6 z( x# }. m - */
' M+ r# a$ _6 {' _ - public int materialsUpdate(Materials materid) {0 I4 Y- [$ Q& w& [" f d
- //sql 语句4 b7 H0 ~# Z( B `
- String sql="update materials set number=?,price=? where id=?";+ Q% F+ _7 |+ s4 K- \4 o
- //
/ C' ~: k+ H8 a4 m! }! K8 c/ t& p - String[]getValues={materid.getNumber()+"",materid.getPrice()+"",materid.getId()+""};& z- H1 a5 h8 J( {! Q0 b
- return MaterialsBase.ExecutUpdate(sql, getValues);8 H7 X2 _- {; m% i
- }
" c! M) Y" T, G# ?) m
' h. d( J- s# e( h- E- Q- }
# ?6 K! j! S) X/ o6 j( H% g
复制代码
" G" {' j! P5 p1 f- ]! L; T代码案例下载:点击下载; i) f: a) o6 D* G- K# F) q3 V' y. A, B
0 l+ R( k0 E7 T/ N& P
|
|