该用户从未签到
|
- package s2.jsp.zhangxiao.entity;
m2 m! d& }! B3 C0 l - /**
6 t" h' O4 y7 C1 Y4 `! c( z - * 物资实体类
1 Q% Z+ }5 c* p1 k2 \ - * @author Administrator
+ r' G# C; E% k/ v - *
1 w/ V8 ~/ }2 N9 k- s - */0 T4 H: p: u O* Y0 m
- public class Materials {/ t9 ^+ ]6 s; b4 _0 t
- // id, name, spec, number, price, producer/ r, \3 G6 ?' k* ?2 ^- Z: k6 N
- private int id;
- S. w8 C7 N, a6 M$ o% L, I - private String name;
) Q4 R$ `) U$ k3 q3 o( R% `: U - private String spec;" y; Q- e9 v, ]% N9 E+ i9 u* S
- private int number;
: U/ } u9 p: w; R - private int price;5 }$ ?% M2 a" Y$ R8 @
- private String producer;( V8 P6 l8 u# c
- /**
' Z! @5 j- `3 o7 C - * 有参构造函数+ ~6 M) c# P9 J' | s$ @" p
- * @param id
0 p1 t# p7 l8 ]# m; _# | - * @param name4 Y* K" f, |8 S( l
- * @param spec
; Z) m5 B5 B, l- d - * @param number: y6 A+ e/ h* U, U/ A. Z/ P8 @
- * @param price# E/ F3 h8 n! g6 R1 h* n. ^
- * @param producer. r3 G5 I) g- B- C& T3 g7 D
- */
/ B" q" {! Z+ F8 k9 t- @. Y w - public Materials(int id, String name,String spec ,int number,int price,
- `- X$ v3 ~7 `. m p) L - String producer ) {# W6 F4 D4 p+ t- d: ]& P
- super();
" e% R/ u" Y4 W! V) P! A - this.id = id;
) P5 w. h' Y1 |6 @( x - this.name = name;, C( }$ L* \8 [ q2 f, O/ R* }
- this.number = number;0 u- ?8 m& L' h9 C. {; t
- this.price = price;
1 |/ X1 n* S: P& O - this.producer = producer;
% {2 R6 P# I( d$ h5 G - this.spec = spec;
2 E* \% O6 N0 k( a7 _ - }
4 t' B1 @! T1 a4 ?0 B1 F - /**
% U# q8 I. k9 `; _ - * 无参构造函数; b s& e3 b# V0 [. C
- */) T5 Z. u E* |
- public Materials() {+ ~) h( o5 |7 \
- super();, @- L8 y% R+ S; V5 J7 q
- }
$ d- b. \/ M7 q' n1 Z9 X0 s - /**
( d7 l8 i b, ^ - * 编号4 z a- }$ N' }
- * @return
9 |& b& n4 i9 }9 k+ b4 | - */4 K0 r' F* g& W
- public int getId() {
7 l; \! l1 O& t9 }& @6 ] - return id;8 o& w3 j- V0 x! X; ]
- }
! s) V/ }- ~% R; s2 N - /**+ |, C2 i2 P5 T$ G6 d. K
- * 编号
. D" {+ `% V/ l' V- B - * @return
- i/ N J4 B& {5 Z: | - */
$ Q9 r8 N$ R- V- O2 V - public void setId(int id) {
, L/ a$ ]6 i8 ]% K7 h- [% c. X: n - this.id = id;5 s% q; p" |' |! P+ z9 S0 t2 q, F
- }" q' W, a& k6 L/ b7 N
- /**
6 ~+ y- T. }. l# k' X2 a - * 物资名称
# X: f! K2 A+ N" z1 C6 Z& S/ Q - * @return& W8 p! h! T3 `" L# L) G& z
- */6 Z4 Q; h! F( ~" w% S: f- ~1 s
- public String getName() {
/ y0 }9 ]( v+ I$ ~; @! {9 L/ s" n$ ^ - return name;4 [0 c1 {: A9 S! B4 O6 w7 m
- }9 F# q4 S# X- m) x+ M
- /**
4 L E( C7 I' I2 ?. d% D - * 物资名称' B" i8 I, C5 e/ q/ k# X8 K1 E
- * @return6 V) }% v; |5 y9 v
- */
! f3 e/ `) N7 a5 t$ x7 r - public void setName(String name) {9 _9 s! k' A$ k$ {: V
- this.name = name;% t O: L- ^' x T: a4 }
- }& B% K) r* T! _2 D* a) a* m
- /**8 ^. T$ Z8 [8 A; ^
- * 物资规格! a& ^. f" w& B8 p* @ c" ~" I
- * @return
. T( Z& }" D( r& r - */2 y3 P' C: z( `( u4 x" Q
- public String getSpec() {( H7 O1 r( n( u) G
- return spec;
+ D1 ~) l6 i# Q& |4 w - }7 D9 }( M( z# X8 b0 x2 Y Q8 G) k
- /*** `* Y! e7 ?2 @. C3 v, k2 A! V
- * 物资规格
4 {7 o% s' {' T - * @return
6 h! `) g- ]( ]: q/ \. J - */
4 S, g+ |* x) e: X4 t - public void setSpec(String spec) {
. b! L2 e* {5 N+ Z7 h2 y% g3 n - this.spec = spec;% Z1 C j8 Q0 L T1 P6 n
- }- ^ M- [4 m) c) @* q% \% ]' U
- /**$ K8 ~( A' G7 u$ k9 | [& ~, t
- * 物资数量
& M2 c0 M- n& X c. W' [2 N - * @return* p$ g+ O1 V! v
- */
, j& K8 x# ?, Z/ \ - public int getNumber() {; m! }) U# L! O0 A3 b6 T% y
- return number;
1 d. g" j Q8 C. P0 ?1 N7 Y! ` - }
$ S1 E! B8 c, t, ~ - /**, Q; y& o2 v# N8 W* `
- * 物资数量& y" u% h- O" J6 a
- * @return, O% g% n3 _ l. ]
- */
& D; v4 \. F, s0 t7 `8 V - public void setNumber(int number) {1 k: o! H1 D& O8 _ x9 y& K
- this.number = number; Q8 b. U+ D: @( l. P) \( z# c! S
- }
0 t, g; G% E8 r: ~ - /**
+ o1 z- }4 W% S6 u - * 物资单价# Z5 o8 \' u+ E% ^% x; n
- * @return
9 j2 v; _! P# p# D2 ` - */5 ?$ @. }; f$ y" [2 u/ I2 {
- public int getPrice() {
6 ?: W" B ^/ t" k$ K7 v( ^! s - return price;0 J3 [2 Q3 ]0 x" K
- }7 Q4 U! V1 I# p0 E5 P% |
- /**( v! Z" e$ q" i7 |
- * 物资单价
; j: d7 G9 j" l) t - * @return" d5 A% \! M4 Z) ^9 j8 i' Q. j
- */, K: k: H) ?2 s# t7 Y
- public void setPrice(int price) {6 B: P) h6 ?5 B3 ^
- this.price = price;3 C/ C" w" u; ?+ l4 a) t
- }
0 S$ R; H' [; p2 U' a! G - /**1 p: @4 V8 F" o$ S
- * 生产厂商( `3 h( G& Z# h1 H+ _
- * @return. c: S0 z& D E4 x8 P b
- */
9 J5 ~ x+ R1 A, { - public String getProducer() {
$ t, t2 N, Y% C7 U& |3 L - return producer;
, L# s. n3 I" q. Y, ~3 l - }
3 y* I, U- {3 F T k6 H2 o1 W V# L - /**$ A) ^! M# i0 Q. }
- * 生产厂商& b' B" q6 y' G2 q; M
- * @return9 R. ?& z2 D8 l g# @1 q' M
- */
% z( D% A+ M4 [$ l1 o) n6 Q - public void setProducer(String producer) {
- E$ v* [% x# N" v9 A' K& p7 q+ W+ g+ M - this.producer = producer;! H% K3 `9 R" A$ r) K+ S' w
- }
3 l" f- A2 X @- w% ?5 a5 H - 2 k0 M; O5 K# K: U! q5 N9 q' {- M
- }: ~4 h3 m- W# B* d( q" a+ ]% o. x
复制代码- package s2.jsp.zhangxiao.daoImpl;
) l6 C0 ~, Z9 w- J4 ?: \3 g - import java.sql.PreparedStatement;% _ r/ E, R6 g* z' o* b% G$ [
- import java.sql.ResultSet;5 |# B- Z" H, O# D
- import java.sql.Connection;) c( `$ ^, v: E+ F$ Z" L
- import java.sql.SQLException;
4 ]; B7 ]9 ?4 G - import java.util.ArrayList;
5 Z3 e- Q9 {" q - import java.util.List;1 ]0 m0 Q8 Y( [
) [! y) p ~9 I! @$ X- import s2.jsp.zhangxiao.dao.MaterialsBase;7 y# D# R% E/ `5 a4 m4 Q: t# _
- import s2.jsp.zhangxiao.dao.MaterialsDao;$ ?+ T* \- r% ~, ]# o
- import s2.jsp.zhangxiao.entity.Materials;
b# d, V3 u4 T! L) L - /**7 p8 `" e/ O3 o% u$ R* A; S
- * 实现接口的方法
6 r7 T5 C" c% s3 b5 H3 `" z - * @author Administrator% }6 ^8 G; C) M2 e( z' z
- */ J. f! Y/ Y0 A
- */
$ R( h5 b) L# C" A! e F# S - public class MaterDaoImpl implements MaterialsDao {
6 r: v1 w+ [+ W6 D) J - /**; q5 q. o: U: Y: ~4 C
- * 查询全部
! [" i+ |$ @# ]7 R$ U! ~ - */5 A& g* K; P4 ~% {. u
- public List materialsAll() {
& g7 J, E3 A2 [, O) E4 W6 b - List list=new ArrayList();/ l; ^0 J/ l3 G% u! c
- Connection con=null;
# ~* H$ D0 l& v' J, q' ~ h - PreparedStatement past=null;
1 f, E' c0 J* L - ResultSet rs=null;
0 d0 N8 e8 A; Q - //sql 语句
. M- ~8 T5 q8 s* n" m - String sql="select * from materials";
! y9 a0 |. v8 ? - //连接数据库9 x# `% W5 r3 N9 k7 P1 I
- con=MaterialsBase.GetConnection();& S; L0 h! x& [7 `; M1 l/ S
- Materials materials=new Materials();7 d! Z! `& E6 x$ ]+ `) c
- try {
: g% I* w3 O: o - //传入sql
+ u8 e* M t" B2 p: [4 h& ~( t - past=con.prepareStatement(sql);
. U) J- C' V& U - //执行方法' N2 v9 N* `, W/ b) k: F
- rs=past.executeQuery();
1 i% U; T$ j, Y3 L$ d7 T. v8 A - //循环读取数据
% ^4 {; O9 A! v C - while(rs.next()){
$ S* V; F3 P$ ^: p5 Y* I2 b) G - // id, name, spec, number, price, producer! ?8 D! x% B, w' j- y7 u% U! ~9 J
- //编号
( ^) F3 v. P8 A) F. A - // int id=rs.getInt("id");
; B$ A2 ~" {& R9 Y( J! ?9 m8 R/ o - // //名称
( y+ g: u$ ^8 e( k1 L* V - // String name=rs.getString("name");
* v+ E) C! h9 J - // //规格/ @9 d. E% s% ^8 K4 B$ ^( \! {
- // String spec=rs.getString("spec");
0 s2 K/ c9 n+ J$ ^4 G - // //数量6 N: ~& t- t2 O1 H" ^" V2 ?
- // int number=rs.getInt("number");: s ^" C3 @$ h2 P8 R. o0 V+ ^
- // //单价3 ]6 J6 V/ h& h+ U& d
- // int price=rs.getInt("price");3 V. I. [: S2 ~* D
- // //生产厂商
8 B+ f s% M- z7 Q+ K8 Q& u9 {( l7 t - // String producer=rs.getString("producer");' v* G4 m5 |, X3 l Y6 ?
- // //添加到构造函数" N) F% d6 F( y/ b0 W
- // Materials materials=new Materials(id, name, spec, number, price, producer);/ c3 a; k% Q0 J& a. J8 e! g3 u X
- // //添加到list 集合2 f. T- q/ j) I7 q) v3 y3 ?
- materials.setId(rs.getInt("id"));: z. p0 e" ?2 |9 Z+ \ w M
- materials.setName(rs.getString("name"));
3 s [# N8 j/ }. m* h2 F7 s - materials.setPrice(rs.getInt("price"));* {( F% }9 k: T! E& ^3 }* f+ X
- materials.setNumber(rs.getInt("number"));
! t6 _( u9 q% d- x+ h4 `$ R' ^ - materials.setSpec(rs.getString("spec"));
! U+ g7 h, m- Y7 {; Q - list.add(materials);
4 F/ o+ B. A8 Q" J - }
6 `: R G i l+ r! k; f - } catch (SQLException e) {$ x9 ~4 v5 g( f6 h1 R0 `
- System.out.println("materialsAll 方法出现异常");
2 w+ d2 A. c- Z4 u& Z: d9 @% s% q! m - e.printStackTrace();
1 b& `0 y9 z( R8 }1 C9 g' r7 g - }
3 V2 w6 }) ^3 L' X2 Y - 7 [* A# V8 U) ^/ f- [( M: y# @
- return list;
3 d, }( {4 x, r3 k1 A- b& j0 M' P5 W - }0 x. \7 \- X* ^; k
- ' y7 Y6 z9 }6 _# A" W% p1 C e
- /**
# S, W( @6 y8 R, V% s8 s0 T9 G - * 根据id 查找物资信息
6 t$ Q. C7 ]- }9 A3 k7 ] - */$ p! \5 D$ h* e/ Y" B
- public Materials materialsID(int id) {) X& Z9 C4 h( Q0 c/ k
- //声明物资类( ~7 M8 L6 y {- u" v$ C
- Materials materials=null;
2 h" t: @: v2 H1 I7 B8 B0 f - Connection con=null;% Z: `$ p8 i) R* W
- PreparedStatement past=null;8 p& U1 G" O2 O3 b+ b4 O1 u
- ResultSet rs=null;" ?, j5 E( S2 A' p; f: b5 A Z* C
- //sql 语句
2 k9 J3 n1 \1 `- f* A& D: A8 A - String sql="select * from materials where id="+id;
+ q/ Y6 _$ g9 k0 _7 h- N1 c8 Q - //数据库 连接
. F& g1 k" s9 L; m/ r* ~ - con=MaterialsBase.GetConnection();
8 m- j" z( l3 o$ B% K - try {
|# l& B- v* G P) _# p( `2 [ - //传入sql1 ~" b- n' u. X! G
- past=con.prepareStatement(sql);
9 U( N9 T3 z; R N/ g5 t - //执行方法/
! a) I ] ?/ G6 g0 t - rs=past.executeQuery();
2 m$ s7 Y9 {, c3 D- ~6 b - materials=new Materials();3 c [+ C% C* Q) d
- //读取数据/ h k8 k- W# I% I0 A
- if (rs.next()) {
$ `9 |- Y" Q! [& g1 g* _ - //名称
* n9 r' u; v( y: T - materials.setName(rs.getString("name"));
2 s8 B9 i; o6 w" c - //规格
. o) a7 d" c, L7 C5 a/ r# Q - materials.setSpec(rs.getString("spec"));
9 b+ m' T: C% f; D" E - //数量* c; M6 |6 U" a' X2 k4 b7 J+ q
- materials.setNumber(rs.getInt("number"));
0 k) _, N) h& I - //单价$ X6 I' u) B" f7 a
- materials.setPrice(rs.getInt("price"));& V: _! ]" _. C U" a( p/ L
- //生产厂商
. ]7 _2 d8 R% ~7 {0 s5 Z - materials.setProducer(rs.getString("producer"));% g7 Z8 I- ^1 N- ]( T7 V7 S. ]5 d6 u
- }( u$ G5 }; E! h" l! Z& I9 A# r
- } catch (SQLException e) {
2 @, [! g0 ]0 t6 [1 b/ V - System.out.println("materialsID 方法异常");
. H; f( H& ]- y6 F$ Q) P5 t - e.printStackTrace();
$ {" U' e2 R! B: G8 G* S7 e& ] - }finally
B* u1 l! q2 ~) d0 }- e - {
3 p% w6 r0 ~" `* }2 V6 S - MaterialsBase.CloseAll(con, past, rs);* ^% Q X) @! H' h& C
- }
) o) |, H9 X+ z2 { - return materials;$ v d( Q3 C6 k) g
- }
* m9 \" r4 [6 B; M m - /**
' T: ~5 }5 |6 U; m- y, m - * 更新物资信息, {3 _, k) l7 {/ d6 p
- */8 K% V( f8 t$ K$ [5 ~$ m! B
- public int materialsUpdate(Materials materid) {' i, I6 n) B3 a$ z6 w1 n3 h# Y
- //sql 语句& [- e7 J$ s& k5 i( B7 q
- String sql="update materials set number=?,price=? where id=?";
& L: |2 @ A( H5 k* E - //$ i8 j$ c* S7 j( {7 z
- String[]getValues={materid.getNumber()+"",materid.getPrice()+"",materid.getId()+""};2 Y; t% R, f9 l0 o! S
- return MaterialsBase.ExecutUpdate(sql, getValues);
9 i' l. R7 q9 N* k9 h g - }
- \: o; B9 N% O; Y" h, |, x
0 s9 b9 b3 ^. f' {- }
" [" m" ^5 I( _
复制代码
# X9 ^. D7 O% }* T代码案例下载:点击下载
$ P& X4 W# t B8 d# S& v" M6 {/ ~, Y5 G+ n: x9 G* }
|
|