该用户从未签到
|
- package s2.jsp.zhangxiao.entity;
8 ]$ Y" `& S9 p- B7 }8 g! ~: _8 D1 r' X - /**
4 J* _/ U1 x* B* l. t - * 物资实体类0 d) i! H/ D2 u6 s/ J
- * @author Administrator2 i; r- l, s5 V3 h& C' w0 G' V! ~
- *
N, O! [7 Q- F* j8 A - */
& J) C2 K2 k6 b. ]6 n) c - public class Materials {
7 Q2 c V( T3 Y - // id, name, spec, number, price, producer; j/ x( `- e6 n. D
- private int id;: A- J% J) o$ g$ f, s$ @
- private String name;; T4 ? t) J2 a8 i
- private String spec;* A& S. w/ L- H& }: b1 \9 j4 W
- private int number; {$ o- J$ T$ C! S7 Q7 G
- private int price;2 b# N' o9 `. T0 ~- a7 \
- private String producer;- Q" b( B0 }9 [( O G' y+ D
- /**
9 ~. d' u" u& N9 e: D3 o - * 有参构造函数* ~2 ]5 b- m" G3 ]# D' f \: i
- * @param id6 Z# \8 r j6 ?" i7 k- m) U
- * @param name0 b! j, a2 a/ m- y
- * @param spec
0 Z( j$ ?; Q: D4 T2 e* w - * @param number. v* z6 y0 X! c2 S/ K
- * @param price% @' G9 k9 ~# o/ s$ N F0 [
- * @param producer& M) F! n9 u3 G$ e. X! a
- *// ~1 v2 k1 y+ i
- public Materials(int id, String name,String spec ,int number,int price,
# w& u/ v: A0 Y, b- T6 q - String producer ) {
, x9 _8 s* ^; D. {, N' {& n - super();
H' g. e) n N/ K' p - this.id = id;
" [% i+ a4 z+ _) G - this.name = name; x) c* W, n6 H- A* N" Y' q$ g
- this.number = number;$ D9 z8 G1 S0 c+ H- s
- this.price = price;
) s3 d/ x$ @; K- F, V$ l - this.producer = producer;4 ]% r) v# M2 z5 V, m3 L; k6 o$ b. e
- this.spec = spec;: \% W' r1 x) i$ |
- }
3 j F8 w1 A3 y - /**6 H0 s! z8 }) E- o, D' ?9 i! M+ k
- * 无参构造函数
7 c; g7 q2 y4 v - */
) h a$ p& N% ~. Y, r- N - public Materials() {
) H5 M: m0 Q: O# s3 p - super();
/ o# e- b5 J; B% }! R - }
- f) A0 M( y8 h* j; x - /**
& t' d% [3 b- C* Y' ] v& W - * 编号% W9 e. ]: S% M: ]$ [# \: R2 _
- * @return
4 H/ U; [$ n+ G& C+ J - */$ U/ b4 f3 k! P3 V
- public int getId() {
. E& Q: l# J# ?& l$ [ - return id;& d$ N( o; F5 k3 o. k9 Q) X
- }/ i9 s7 P; b7 q: j% w! Y+ _
- /**
6 |8 P. n& D3 s' E3 X$ v - * 编号4 W! W6 u, i! Q8 U. o" I
- * @return- O1 x8 C& O' l- D
- */
& U8 ?+ m8 q+ I/ Q y0 M$ R - public void setId(int id) {
' a# E$ D- _' B3 J* T - this.id = id;4 ?" ^$ b* p. g
- }
' s( n- y0 f# H8 F# }; [ - /**. h1 E' g/ }* n5 L9 @
- * 物资名称
5 d# |1 T! f: S R: S - * @return) w0 k, Y% U% E2 Y, |1 w$ R
- */. z" L: E! d2 G9 k# l2 P
- public String getName() {5 z0 Z/ D7 V2 _/ n
- return name;' q/ Z$ t& d7 e: F1 U
- }
. k2 b2 [ V4 g - /**
( F. G7 ^: l. g5 D- q - * 物资名称/ K# E( r$ \0 j# `- ^. N1 [
- * @return
3 h7 K5 @) p. _$ c) I p! ~ - */3 c8 y% B. K2 U/ x: A4 D6 Q2 Y- d
- public void setName(String name) {
6 e+ x7 G4 U- n/ h; s' ~* ` - this.name = name;
1 @% Z& ]1 r6 w4 u7 r* ^ - }
+ L+ B; k$ L& h- D1 v2 | - /**- u0 X2 M1 z' @
- * 物资规格1 p2 R8 c( M1 _0 E. t' A6 v9 t
- * @return4 ^ H/ [) w. d6 @6 B
- */3 l+ m9 P& ~+ F
- public String getSpec() {5 [5 m# B0 t S; A3 B
- return spec;) |- F( U. U( v: E( D2 g* t
- }) M; S" P! ^6 v0 N. B
- /**5 e6 J# _. Z, s. D, [( x& m
- * 物资规格
+ D! a G. c6 n% Z6 B - * @return
: p. H N; i8 \0 b( n, T* H - */ n' p# t0 ]" N0 v: \% K! d1 O
- public void setSpec(String spec) {& W/ h. ~4 Y% Q/ J- L
- this.spec = spec;/ W6 o7 ~; ?5 e
- }
4 A3 R/ `+ S1 l( p& P; ~- _ - /**) @3 B W& S+ _$ ~
- * 物资数量6 b7 q! q0 W! \* ~; {" r
- * @return8 c+ u# G+ ~; K4 C
- */
9 o. D3 E, N4 G4 d! C6 M! R" X - public int getNumber() {
* A. D' t3 M* U$ o- d0 ^! v& m - return number; h7 Y9 C/ ?4 s! h
- }
* Y% H5 ]5 Q w1 `6 w/ F; k - /**
0 _8 ?7 Q- c0 P0 N: o- Z% J9 ^ - * 物资数量' P# f1 {' ]6 _2 h% L! H- G2 O% o
- * @return2 J3 c0 m* j) N; ^9 }9 _
- */& v) [6 Y: B2 L3 B" n F+ g
- public void setNumber(int number) {
2 j. A) ~) ~; t: @$ k& r* n9 D - this.number = number;' H5 {+ G9 V) p
- }. }/ w# n- J" x9 Z1 F) W) X5 Y: u
- /**
; y; I: m' s Y; H* k2 y - * 物资单价
; J" U4 A- F, H2 z - * @return
1 _/ f) d/ D5 v - */- H+ \: Z9 g( q: Z7 [3 a) x
- public int getPrice() {
9 j* B+ @' D8 Y- t* Q7 f - return price;5 Q+ a+ j7 ?3 c2 `9 P
- }4 x: M# u- A1 ~( t0 r. L
- /**
) b3 _2 A& o2 {" d3 z! p9 d6 \2 d - * 物资单价. `) d6 @6 i! O5 D8 ]& G
- * @return
" o# j/ q6 m" B& c$ y! L - */1 _) P: \2 \! Q, m! E/ l5 ~
- public void setPrice(int price) {( W2 D3 j; L. ^- h8 y, }' z" J
- this.price = price; H" r8 ~8 K8 G; b& s' p3 m. e& P6 L8 y
- }
8 ]0 B+ I; m# N( H* h" i; [' e - /**9 Z8 ]' v, X6 d
- * 生产厂商2 e& l6 \$ N" S0 Q. d
- * @return% B* r8 G& X' s2 t' E1 Q3 Z8 D* Q
- */) j. v/ w! ] e" G0 T! C
- public String getProducer() {- d0 q- e! N8 G( w: a# [
- return producer;0 z M" J; |/ y; _
- }
* F; ], r! ?4 Z& }- }, H - /**$ @2 H% @0 C0 T4 O( i* d6 \, G
- * 生产厂商
& C2 n( z$ G, t) C; | - * @return
" g% o! ^ g4 ~% ^! X0 H* _ - */
8 }( |3 `$ b2 M7 k - public void setProducer(String producer) {
# \) Y" p" [5 z7 `- E - this.producer = producer;' |0 C. ?# V9 R' B2 R1 ?( Y
- }0 W1 b! n5 W9 l b$ g. u- O* ^
-
D( v5 F$ v' a% ^" M" E# r - }
- ?0 @, |+ K* z) b% i, Q2 E! L$ }
复制代码- package s2.jsp.zhangxiao.daoImpl;) s) X5 K: X+ O" e( ^) h+ M
- import java.sql.PreparedStatement;7 v# Q- K T e3 q/ q0 W) s
- import java.sql.ResultSet;
1 U/ d( @7 o# L2 i - import java.sql.Connection;
, }( c4 O# ~. V* E' J - import java.sql.SQLException;
# N3 ~& o& ?, G3 e4 n( U - import java.util.ArrayList;* E" E: M( K# ~8 m- r# j
- import java.util.List;
0 L) w3 m7 t/ v6 n5 N) { - % Z, p& ^2 I( r4 Y8 t) |
- import s2.jsp.zhangxiao.dao.MaterialsBase;
( r' @5 t' m- B2 a% K - import s2.jsp.zhangxiao.dao.MaterialsDao;
5 a: X8 I8 z: I! m9 T - import s2.jsp.zhangxiao.entity.Materials;! n4 k' Y+ A' E9 D* T4 y: G1 Z3 S
- /**% n: p. E8 ^. S- I1 Q) b) m: s
- * 实现接口的方法
8 M& {: ?- g2 M8 Q6 ^ - * @author Administrator4 M% I4 v) `% }* p8 }5 l- M
- *
9 u. Z8 ?, o, s; G$ d6 f- y$ Y/ H - */
6 @, ~6 g& f3 z6 h& p1 q z) d1 O - public class MaterDaoImpl implements MaterialsDao {
4 n6 A1 m0 s1 S& L - /**" p2 o: }" |9 |( D
- * 查询全部
" |1 A9 I; P+ _5 q$ }( p - */
& x' {! r6 q6 i7 C0 Z" P - public List materialsAll() {
* x, Y7 ^& N! C3 m - List list=new ArrayList();/ s2 R1 o& _# F. f
- Connection con=null;
7 R% G/ `4 Y& b6 Y0 r2 v - PreparedStatement past=null;
n) n: f/ q) Y& n; i - ResultSet rs=null;
) c0 C1 `2 E6 | z. V - //sql 语句
4 Q- w4 R6 k3 ` - String sql="select * from materials";
% Q# y* O! r$ S5 y, @; G( J - //连接数据库
" t) e$ n( z# u; x6 t# |0 R% d: t - con=MaterialsBase.GetConnection();
2 z; w T) o1 o3 b - Materials materials=new Materials();: g) o6 {4 L$ L: ~( x
- try {- u; I) t& u1 c$ P
- //传入sql
/ _7 ^. ^' D5 @8 i' k. K! G" e* J - past=con.prepareStatement(sql);7 q& i8 f& I$ C% a! J# W
- //执行方法; X+ o5 l9 s' @7 E1 f+ A
- rs=past.executeQuery();' d% D6 I! Y7 w' n; ?
- //循环读取数据- v- f' l9 k9 }6 Z) k6 ?
- while(rs.next()){1 K* o# H, W# Y8 c2 D
- // id, name, spec, number, price, producer2 g6 N N7 C4 {0 @4 h1 W1 p; O3 b
- //编号2 R0 a8 S ]+ F3 y
- // int id=rs.getInt("id");" I9 C* B+ P' H+ j
- // //名称; U- F. e1 `5 x. ?4 F' ~$ E0 N& |
- // String name=rs.getString("name");
5 ?5 J2 A# X; N8 \6 n4 T - // //规格
; V2 ?$ U0 U' q" n) s - // String spec=rs.getString("spec");
Q3 `! D) x8 X - // //数量
; P2 _5 E6 E& j3 @ - // int number=rs.getInt("number");
, U* P/ s% f, j! g+ \8 l5 ]; g7 d - // //单价* Q3 V1 R6 H4 [2 n9 I3 G& I
- // int price=rs.getInt("price");* L3 `3 R7 a ~ K/ `# _ a. y) ]
- // //生产厂商
1 M, e; d: R+ K( g0 m' ~" } - // String producer=rs.getString("producer");8 t5 N8 r: ~! ?, {8 W1 b
- // //添加到构造函数) ], M; }3 w0 z' L7 b- a) y* a
- // Materials materials=new Materials(id, name, spec, number, price, producer);+ s$ v* {: ^, E9 h7 [2 D/ H/ ~7 h; W
- // //添加到list 集合
' g( m7 P- W# W/ R# M. l, K- i - materials.setId(rs.getInt("id"));9 o; S% q3 e; k: G- C0 Z. I
- materials.setName(rs.getString("name"));2 o) a' R8 x* n0 k
- materials.setPrice(rs.getInt("price"));
/ Z; [8 M7 m& e1 j# F - materials.setNumber(rs.getInt("number"));" R9 Y! O2 w) w+ B( n2 b; ]
- materials.setSpec(rs.getString("spec"));$ B7 M) `5 i: T( X
- list.add(materials);
6 c1 a6 p0 Z* ^ - }
4 z/ }% K. `) L( N6 ]( k6 V - } catch (SQLException e) {; J% f: {- z r9 M
- System.out.println("materialsAll 方法出现异常");5 W, e4 B \% Q1 m& V" s
- e.printStackTrace();% U- h/ I& ^. n; D6 E
- }6 k" M+ Q9 k$ l
- 4 i# E% L2 e$ k2 t8 a( f) }. A$ H
- return list;
4 Q7 M/ T; l3 L ~: \% O - }
2 U/ g- Q F) {1 d6 |- H - : R r. C0 {( z8 Q
- /**
" _9 f: ~8 o) M) z4 p - * 根据id 查找物资信息; s9 k5 o; H) L/ P) b
- */! v" y$ X1 c4 W. J9 x
- public Materials materialsID(int id) {
) K. S* q5 I1 {3 ]; h' E: ] - //声明物资类
- r ]8 d' P# U - Materials materials=null;1 r2 {7 j* K8 I5 d
- Connection con=null;
7 o7 R- A/ p' e - PreparedStatement past=null;
2 r) r) q" E* J* o* a - ResultSet rs=null;
1 K1 R0 l F5 |- Q2 a U: e - //sql 语句0 n" y; |' g, Z
- String sql="select * from materials where id="+id;
3 n% k$ Q L# b5 x) ~. D - //数据库 连接
# ]; V( _8 w8 Q- n' O( a- m5 E - con=MaterialsBase.GetConnection();7 @( |( z9 c' y$ g$ E/ \& n
- try {
5 j8 X5 a# ~( p/ A4 K6 y" i6 u+ J* Y8 H - //传入sql9 p. \3 I/ ?8 ]0 O: q
- past=con.prepareStatement(sql);' C- h7 z* V9 A7 J$ S a$ l
- //执行方法/
& D& {, i* n% A+ X - rs=past.executeQuery();: ^5 _5 M7 |: \+ A: g* x
- materials=new Materials();
- [) L1 J5 `6 ]( k* r ~+ b - //读取数据' t- m' c/ D0 H' G0 m6 E
- if (rs.next()) {
3 F% s/ a. n. ?! G - //名称
]. T% Z2 \' Z) L; h/ ? - materials.setName(rs.getString("name"));
9 I+ J8 N. \0 J# b# r2 j7 l9 V6 \, C. R1 ` - //规格, I' ?2 X6 `3 [/ O/ ?- L n9 \
- materials.setSpec(rs.getString("spec"));
; N" w( ~3 e4 D. C - //数量
$ R3 _/ G, P6 f' ] - materials.setNumber(rs.getInt("number"));
9 e2 e; R$ K' ~$ V% z# Z - //单价) b8 e0 E, s! X! q x
- materials.setPrice(rs.getInt("price"));" W5 p, I' `5 r" r B- H
- //生产厂商
( z. d1 b4 a, n" ]) S% H9 S' k$ L+ V/ @ - materials.setProducer(rs.getString("producer")); ?: L/ K! ^* D
- }( X$ Y- z; }' C; w% B! n! k
- } catch (SQLException e) {
! v1 U9 p/ D0 t/ E* C9 ? - System.out.println("materialsID 方法异常");
8 m) {! |" D8 a/ f: ?0 r/ A& ?% u - e.printStackTrace();: w3 b' g' l# U- b* A. @" }
- }finally
7 x! Y% O( X/ R2 F - {; P" v! \; s, A/ K+ p
- MaterialsBase.CloseAll(con, past, rs);
" b# M" ~3 x" @+ K x t4 h - }
' y# g9 @( v5 H. E' h! o1 {) ?. K - return materials;8 }' ^/ q& Y* C1 u* c/ `5 s" {
- }
$ O$ ^8 `2 k, |$ y0 m9 O+ f% m" r. N - /**
2 p$ M& z" ]/ h* f1 ]# { - * 更新物资信息, T2 B# r0 q/ A; y9 \- H! I! k: B
- */0 Y! L0 [& x, q
- public int materialsUpdate(Materials materid) {, Z9 o3 F( y! i( z8 |5 F
- //sql 语句
0 Y0 `- y+ |3 J/ y+ t - String sql="update materials set number=?,price=? where id=?";
8 `' F) e2 p( {1 i/ ?: r8 B' k1 U - //
2 m: n6 Z. A, y - String[]getValues={materid.getNumber()+"",materid.getPrice()+"",materid.getId()+""};3 \; L' a" B u+ C- J+ y
- return MaterialsBase.ExecutUpdate(sql, getValues);" p$ l; V* ^7 N3 D' s
- }- b+ T% m, [' q$ T$ o- ~
- " j; D9 c8 w3 k# M5 L
- }
6 S0 O: h- d- R: H C. |
复制代码
- D5 F- S4 A2 A/ `! @代码案例下载:点击下载8 S7 j& d! ?$ H" |# g; \4 v
* b; v- a1 ?' V* q# c6 U
|
|