该用户从未签到
|
- package s2.jsp.zhangxiao.entity;- b3 P3 ?- r" K# L
- /**
2 }. _! {4 S5 ?4 M - * 物资实体类 h! g/ p% t9 j o
- * @author Administrator7 i# N7 O: r, b/ q1 y
- *0 {! y1 O4 F1 S6 S
- */( j5 Z; w! a7 m* E1 L+ { }
- public class Materials {
0 z( O/ h5 i) Y, R - // id, name, spec, number, price, producer
) q: q- ~9 g; Y1 I( Y - private int id;9 w% e8 c! L# `. n4 L- R/ F0 M$ \0 L
- private String name;1 q1 P z+ Z; d# a6 ]8 v0 o" p
- private String spec;
- [1 N' T1 R9 w7 r4 Y0 B - private int number;
5 k' U9 e$ [4 \9 ` - private int price;
' @' x" G' Y( n- g" R( S. A - private String producer;+ I0 ?( j3 m+ |0 V* f' S' U: }! I
- /**& z8 r5 ^5 q2 n5 Q
- * 有参构造函数- ^0 ~4 S6 X9 a( |! H" C1 ^
- * @param id- V$ }+ _( A7 s7 x; M2 ~7 p$ @: P
- * @param name' H& o; ?: k. f) E
- * @param spec
0 K5 z/ J; L; d1 Q9 W( `1 c K5 e - * @param number8 C- o8 P# I0 e* T- p: @9 T
- * @param price* V3 A- h( W; U- U x
- * @param producer
. A8 o3 R J1 N e. G1 ^8 ?# P - */
* t& m; J& O L9 m2 k' J - public Materials(int id, String name,String spec ,int number,int price, 8 J# B3 s5 B5 |1 U+ S
- String producer ) {6 ^ T: k6 X3 M( j6 u. H% j( l6 C
- super();5 O" u, n# O: ]0 {6 ~' n% b
- this.id = id;6 U- O5 d" d7 ]/ a! n6 H
- this.name = name;$ y* n$ g- y5 M$ n$ T2 {
- this.number = number;; }8 \- T1 n- ?+ \: s
- this.price = price;
0 j3 z* }; D: `+ y - this.producer = producer;
f2 b1 X+ Y h0 M+ A" X/ ^ - this.spec = spec;: S% l1 L& E0 l! j0 L# `
- }7 F+ g) q9 U" T6 a0 l
- /**
$ C# I: E4 N6 O+ F - * 无参构造函数
! z3 Y! \6 g" F j* o2 N - */. `7 Z! w2 h7 M r) b, d0 l
- public Materials() {8 |/ M! l) r2 F {! S
- super();
0 D. l: {" j* p4 x - }
4 {( S. I+ Z K3 Y" O1 r- A' D - /** V/ J) l) H$ [0 C
- * 编号" H. d7 ^$ e$ Z0 x" B% [
- * @return, B$ @2 G; c6 a- M+ `
- */
/ ?; u9 Q- p5 V) _: U8 p1 U - public int getId() {
; u& t# D2 @6 f% e% E( W - return id;3 a1 O" a& `$ R8 G& ]! e @9 V7 K
- }
2 w/ H: U7 {6 M" o6 r+ x: r4 I - /**% A& B, X" S r J5 ~& K
- * 编号
; D: v# y! @: ]0 Z - * @return
/ p4 y. }' G2 Y# P6 _ - */% ~) a8 V& p1 r0 }9 H& s
- public void setId(int id) {
& z: Y# h& C' o! U6 w a X - this.id = id;) i0 R J) [% D8 I
- }
& s& |! B; v. \! [ - /**0 F' M0 L" l x4 ]5 @7 }8 J" o
- * 物资名称/ f( C! Q5 w8 i0 q
- * @return
0 k0 q# a- ^8 r) ^ - */
9 g7 ]8 W8 G. f - public String getName() {8 U2 R% h6 L: y" D
- return name;
/ w3 n; L. \* T7 r) j) g - }% D# }+ I! q& ]: O8 t% C1 d
- /**
8 i2 A! B+ k+ G/ }. W9 M# Q' v( r! o - * 物资名称! L [4 Z1 K& s8 U7 Y0 H
- * @return
; W1 v: S- {/ R - */
+ Z {& `5 g; G: N) u' _4 b - public void setName(String name) {
1 U/ ^; P) C6 P7 X5 b - this.name = name;* F: u1 g5 X8 x' p" M* @
- }
4 i" n5 a/ B! z8 \' r& S - /**2 D1 L: B7 x# x: b3 j- r
- * 物资规格$ w( K) @- I h5 j8 g- ~3 @
- * @return
2 I6 Z! _. G+ v' a - */
: d! L1 o( r6 X$ [0 s' { { - public String getSpec() {
% e7 k- A! k/ | F" z' n* V% { - return spec;( H2 Y) y! Q) Y, Z# I
- }' @! @1 {: c' k
- /**" E5 }* E2 V( ]9 F) {
- * 物资规格
' U B- q$ _8 f4 C+ G' M. l/ t) B - * @return( K8 v; C/ l7 I( `, j5 ?
- */
- J. k! I/ P2 T- m - public void setSpec(String spec) {
! K2 F+ T$ \. _3 R/ Z6 Q7 ] - this.spec = spec;
: ]$ g6 }5 D3 Z M1 N7 ^0 E- i - }0 o9 S) a" K% o/ x
- /**# I+ \; B& E& M4 w* @
- * 物资数量% n& n3 H( J7 P% X
- * @return
- Y: u S$ R6 V' M - */" m2 S! v5 s* t& G; y
- public int getNumber() {
, T% ` v( j- I& h* e0 v+ ^ - return number;
+ N3 ^ W& h6 e' G - }
# U2 [$ W+ k r* p' W - /**& J& k. |" K0 {6 u' }
- * 物资数量
7 K: C4 c" ~1 A. J% ^ \6 P - * @return
$ ^+ G+ P) M+ c" [ - */$ V Y7 t9 u$ j0 h! _
- public void setNumber(int number) {: W/ e; u! q9 N1 K3 ~8 M% F
- this.number = number;
) x1 f4 y: C! ~: D - }* y4 ~, ~1 V/ @7 b
- /**
9 o% ^4 o' F) \ - * 物资单价
8 f' D+ K' b9 o3 Z - * @return
F- A4 E: B T) ^/ n - */1 F8 k C% b6 W9 h; q6 E
- public int getPrice() {
3 `6 T# r1 v: J - return price;" m, n. {' n. J! v2 I4 }; o
- }3 [! i" |/ ~* O# t0 N4 Q
- /**
( A: J7 o9 o: P5 o - * 物资单价5 T* f, g+ j( J
- * @return
3 g+ |% x+ l! J8 d8 @ - */
: }4 j! Q1 B) R9 w) y, g8 T - public void setPrice(int price) {1 D6 B9 l( T2 f, r
- this.price = price;
) P6 n1 _" ]$ `: \ - }% ^& e" W: v6 B" g5 p
- /**, j' u: n( s8 W! A6 |) E/ n
- * 生产厂商" u% I* x3 z+ M8 v" Z# f7 q
- * @return
) ~$ S, ?. Q* J+ v2 g# G4 p - */
. @3 H! u8 P* H - public String getProducer() {6 c8 m2 w$ p+ T- n6 y2 w; T
- return producer;. j& L* v2 E( r' m) X3 j
- }
7 m1 S3 [$ k0 k& `: S - /**
: _/ p$ K. s, \ - * 生产厂商$ s* x/ p; J: [3 u
- * @return
5 l& m2 h' m$ Z" J7 X - */
! u7 v9 b/ T5 m( a/ z1 E - public void setProducer(String producer) {$ t2 q: B3 `& Q6 Y4 y; y+ p0 l
- this.producer = producer;
* U5 u" d9 D* d# a - }
8 z5 _% T+ H* j. T6 t. `5 z -
@/ K q b/ a3 b8 D - }1 Y W( x. P) {: g2 ~0 a: j
复制代码- package s2.jsp.zhangxiao.daoImpl;1 E8 |: A4 Q, _/ U# J
- import java.sql.PreparedStatement;
8 n6 d, m! f; W& T: C/ e Y- p9 s v - import java.sql.ResultSet;: x5 v3 r7 }0 l( n6 k
- import java.sql.Connection;
! G" c/ F2 E0 q; w9 d3 | - import java.sql.SQLException;* I! p0 C& N, T) z5 N. F/ z0 y
- import java.util.ArrayList;8 c0 y% j3 A3 Z A' W) |
- import java.util.List;
. [; T8 f- s: P9 y. U, {
( N$ w; [' X# P) {, P( o0 s- import s2.jsp.zhangxiao.dao.MaterialsBase;* z0 @, z2 S7 |7 t6 ^
- import s2.jsp.zhangxiao.dao.MaterialsDao;
9 j j+ S& B0 ~0 v: D - import s2.jsp.zhangxiao.entity.Materials;
# f4 L8 v# w2 b! F - /** H! x6 c' ^) o# C. H% [- r1 V
- * 实现接口的方法
! o9 G+ l. j ^5 }1 `8 k - * @author Administrator5 V: W- a) M+ @, v) ^
- *5 h& c: ]2 t5 s) B
- */1 |7 s- \- [/ J; v
- public class MaterDaoImpl implements MaterialsDao {& @+ y* |: z) ]$ D1 j
- /**+ j* V+ Z! E0 X" A; U
- * 查询全部" b9 R3 a6 g7 o) p/ |* p
- */
/ v1 n0 M0 R% w3 J( B9 [7 a - public List materialsAll() {
2 [* P/ S i; y4 O1 u6 X0 q9 E - List list=new ArrayList();% S# o* a+ G# S5 a
- Connection con=null;
# O( b1 g5 e5 W( k - PreparedStatement past=null;
( w0 w& |' G3 y( \, H. M4 ` - ResultSet rs=null;" ?$ r7 n. ^9 T Y; C. o
- //sql 语句
! L8 O$ c% v- W+ F0 h! G - String sql="select * from materials";
0 E+ z+ o8 e: t* N% l! [ - //连接数据库
# @$ B$ E9 C2 B" t - con=MaterialsBase.GetConnection();4 C+ i: E, u7 w2 u! c
- Materials materials=new Materials();# S' c# E4 {; E
- try {. W8 L5 E0 b2 [
- //传入sql
$ L4 E# h4 k4 }/ t1 N" j - past=con.prepareStatement(sql);% @/ r+ Y, Y* Z. J& o; C( d9 a( S
- //执行方法
0 j0 A6 N% P1 v0 \/ q - rs=past.executeQuery();. y! Z! q1 w& [- P; a- d& I% O d
- //循环读取数据. P, W: X& K( U' J) f$ H3 _& ]" V" q
- while(rs.next()){
9 x7 }7 a' Z$ A8 Y - // id, name, spec, number, price, producer+ Q8 |, @- \ _- ^" {) A
- //编号
! z) I2 O& d! N$ f - // int id=rs.getInt("id");6 L2 K. e4 U- m J7 x
- // //名称
2 Y/ g) ?% v9 i - // String name=rs.getString("name");
. Y1 i- a/ l2 W - // //规格/ M- ?% ]0 u8 b
- // String spec=rs.getString("spec");
; ^) p; u$ C3 y* w: o - // //数量( e4 ]1 _! B5 S
- // int number=rs.getInt("number");
- v; V) m- L' O" E5 u8 Y) z3 J - // //单价
+ O1 O0 w) `; s" C7 O! A, k8 H - // int price=rs.getInt("price");: z4 a7 b/ R, c5 e0 e3 u
- // //生产厂商
' h/ ] U3 A* Q - // String producer=rs.getString("producer");3 A; L3 o- m- s1 F/ x
- // //添加到构造函数; T2 O8 Z' p# _" X2 f7 q
- // Materials materials=new Materials(id, name, spec, number, price, producer);9 O4 p6 w3 E+ ?: C% y
- // //添加到list 集合
+ R" w& P7 Q8 M& O - materials.setId(rs.getInt("id"));
$ ?1 C' T9 f; B* z, i" n( ? - materials.setName(rs.getString("name"));
+ N6 t$ o' O1 g' A - materials.setPrice(rs.getInt("price"));
9 C2 \ ~0 u1 t: X- |0 `! d - materials.setNumber(rs.getInt("number"));
# r6 _5 f! P) z5 j" m5 R - materials.setSpec(rs.getString("spec"));& a5 r# H( ^7 I. A* x
- list.add(materials);
" O5 M# E$ o7 ^8 @$ A6 H- ~7 x4 y - } $ p" |3 f1 b6 j
- } catch (SQLException e) {
; g& t0 i2 z8 Y - System.out.println("materialsAll 方法出现异常");
5 J# J" [8 R2 T; j- R - e.printStackTrace();
9 W2 P$ f' t8 V! ^ - }
7 t: q" j2 _0 l. r- z -
0 o: M) F+ Y, k1 h( V: \ - return list;
7 r+ h2 p+ C- M8 v - }
5 v' W7 f( r$ y7 a) l - 5 z1 ~# }' f( H7 X% X% |; ?
- /**5 V0 F: ~9 }! h1 H
- * 根据id 查找物资信息# D _8 o0 k5 j" \2 T! R: k
- */6 C$ c2 @8 A5 x) ?) I4 X
- public Materials materialsID(int id) {
4 s' @- o [" T) |8 y - //声明物资类2 V* [/ H9 m) A1 h0 r% L6 R
- Materials materials=null;
& _/ u9 ~& ~- E - Connection con=null;
* Z- x5 ?' m2 ]) ?) @ - PreparedStatement past=null;
; S& V" h z9 k% u5 q+ N) Z: P - ResultSet rs=null;5 h. H' r4 w d/ L+ b
- //sql 语句6 ~& s/ O7 s* R* |/ A9 _1 R
- String sql="select * from materials where id="+id;
; c" M q$ Q8 C3 I/ ^2 _ - //数据库 连接! C/ J$ m# q* l& M" D9 D4 W
- con=MaterialsBase.GetConnection();& e. t v5 L s7 _1 B
- try {" B+ L0 B! E! t% v; d* w; p8 M( p
- //传入sql$ t1 ~, R# \0 S4 i/ K* _
- past=con.prepareStatement(sql);
7 y9 n( @; ~' b4 P - //执行方法/
2 c4 S% n% W P( Q2 ]0 A) K7 J - rs=past.executeQuery();
9 M" M* r$ b' j5 `2 y6 L9 R" E - materials=new Materials();
* r* Q9 ]7 I- b6 I - //读取数据
) a/ ]6 l5 q- O1 k8 ]" \ - if (rs.next()) {
, f1 p, @: b) m6 v' z - //名称, }6 e0 y/ t. x5 R( k- F
- materials.setName(rs.getString("name"));
; v% J% S: y: [7 Z$ n2 R+ G7 M - //规格
9 Q/ y; h, e# J, K* G, C/ B - materials.setSpec(rs.getString("spec"));1 u8 I7 _. \. X6 p6 Z @; c
- //数量2 c% T$ L0 B- W( ~) B8 ~; ]' ^
- materials.setNumber(rs.getInt("number"));
- _3 i, c+ H4 _( ], D# x+ y# z - //单价! J. d7 ?# V0 A! \ d
- materials.setPrice(rs.getInt("price"));
' [ }# f% q1 y" n- x0 T0 L - //生产厂商3 B( u, w9 \/ B2 w: W
- materials.setProducer(rs.getString("producer"));2 Z0 _0 S$ ]; h/ o- o& q6 {
- }
) |- t0 n1 X( F0 H e! w# Q - } catch (SQLException e) {
+ W0 e3 x' H2 L+ N$ _4 Q; n( \ - System.out.println("materialsID 方法异常");
: v! c9 z4 I$ `0 }5 H - e.printStackTrace();
+ c2 h4 t D$ z - }finally
0 X/ u* q; n* [2 s. z1 P/ S/ h% v - {/ O8 \/ v0 Y8 e& R# J: r) \1 }
- MaterialsBase.CloseAll(con, past, rs);
; H' T) S2 I( Y/ j% {7 Y - }! K. ~" U0 V* U4 [4 ^
- return materials;) Z3 y2 v) r% H, p
- }' w- x) z0 I0 t( @ C2 A
- /**; D: k. Y* w) @2 f
- * 更新物资信息% t% l s$ L a
- */# @' r' a3 e2 e6 S8 d
- public int materialsUpdate(Materials materid) {& w. p2 K( C+ w- k7 k4 @+ r) X b
- //sql 语句
+ T2 {$ u/ G. O( s - String sql="update materials set number=?,price=? where id=?";
$ z( t5 I/ Z5 x8 |7 N" u6 Y# z, { - //
! \5 ]' W$ }& o5 O/ } - String[]getValues={materid.getNumber()+"",materid.getPrice()+"",materid.getId()+""};
4 C, K) n/ p! @8 a1 f5 x - return MaterialsBase.ExecutUpdate(sql, getValues);; B! E3 [4 Y. b& C( A+ f( r
- }$ D/ o7 U' U' H9 h
- 6 X: S4 W0 |" Y/ `$ p9 T" k
- }
8 j3 A B0 s( ~+ R/ ~9 j) h
复制代码
0 U6 o5 k: z' a) U/ d, F# W代码案例下载:点击下载( P5 P5 k5 a1 Z( B) P/ J
6 v: u L# g% m |
|