该用户从未签到
|
- package s2.jsp.zhangxiao.entity;
; o" U, C3 V5 a% }# b3 |1 _ - /**8 N! M8 H1 j( k
- * 物资实体类
2 F# P p- g6 o4 L: F8 U2 [ - * @author Administrator
/ H7 {; o; P1 l! ~' q8 u - *
' O& F0 C6 s4 b2 J4 R% h - */
1 S+ S! [" f2 X m - public class Materials {
- |+ R+ _4 ]/ C/ S - // id, name, spec, number, price, producer
# L! P6 V- t1 `' L* D8 P - private int id;
. l' x8 Q; Q2 B/ v - private String name;
$ f: f. `) n5 r; r3 u s - private String spec;9 ?& f+ c! N( V7 h
- private int number;
1 s5 q$ B5 ^4 `; E( d) O: G9 O - private int price;
6 X0 r: P% b+ X6 j$ O4 l - private String producer;5 P8 Z* o- I9 u3 t
- /**
# V# ^2 W9 }7 Z - * 有参构造函数: ?7 K9 |2 y3 u2 r& e
- * @param id1 O" O5 G$ C# H4 i
- * @param name& f: H# {' E/ h2 F# j! a
- * @param spec
7 d7 l6 T* G$ y& ~! J ` - * @param number
V7 S7 w: f/ C( T5 ~$ b - * @param price( G$ p! c8 j9 J& r e+ K
- * @param producer. o; N3 Z! T0 V; @
- */* | E: F# J$ ~, C& T
- public Materials(int id, String name,String spec ,int number,int price,
8 m) `; [2 ]. z1 K* H# L - String producer ) {( |# E- z0 V) E2 e W
- super();
; X& s. t) U+ b( [9 I& ^+ R - this.id = id;
5 l& m, k: ]: o% Y9 L2 l - this.name = name;* `# {2 d, G6 |' W; d' Y
- this.number = number;$ Y, h* k* T r
- this.price = price;( \9 I! z8 y2 C8 Z& ?8 M
- this.producer = producer;8 _- V* p& m; V" B
- this.spec = spec;' K8 q4 w M6 u
- }
( K2 {3 k( F( ~7 I - /**
8 s6 q* B1 i* G {, w+ Q' U( k; P - * 无参构造函数
4 R9 l6 H" r) M - */
9 A8 K. u3 A3 x9 ~9 B4 C - public Materials() {2 O5 L3 H3 N& o; l8 G( f8 ~1 z
- super();8 ?1 J) O1 q! K5 U. J: T$ W# a& c* L
- } ^9 Q8 x8 V* z r5 v, i& y0 k9 P
- /**( ]: q/ o/ I- r# e8 T. F3 o" J
- * 编号- j% N: Y5 D" @
- * @return8 D+ ]- Q. K$ b# H6 F3 ? b4 |
- */
' F. Z( o1 Q" O/ f7 P q8 M - public int getId() {+ k: ^1 ^: X, q" L# C- q% l' Q
- return id;! { {; Q3 e9 X) `( F' a% y ?
- }5 p# d! W0 k& Q/ p* }
- /**
9 a) ?5 {+ U( H( j, p$ A% J* ^ - * 编号: `: Y3 t* g" c# Z$ c, v6 y8 b$ R
- * @return
, p7 U C! U/ Z$ F" v - */+ r8 B3 a* p5 c" F
- public void setId(int id) {, s: t/ S: `7 p1 z% f+ [
- this.id = id;
# A' n; D6 x8 h) L u) y) {/ I - }
, r% f3 z; c# h" Y( E. r - /**5 O2 w& d1 M! M& M. U
- * 物资名称
( C( S8 _" L9 S$ ~4 U+ C - * @return
) G, P& A2 l' z7 m4 f7 Z - */- |) |7 K+ c$ ?4 s/ p. Y
- public String getName() {
- O8 ?8 ~8 f- j. q1 `* N - return name;
3 Q' _0 ?; z& r( [2 U- w( K - } M0 \3 J( _) r! }( o
- /**+ N8 g6 H" L) [$ i
- * 物资名称
% W* [3 m) _$ `" Q4 K/ Z( o; @1 T - * @return2 O5 X0 N- m1 @- [. O( G
- */; w4 G- n* M6 V; J
- public void setName(String name) {/ O. y5 _# B! n( ?1 r+ R$ W; q0 m
- this.name = name;
, l3 l0 n+ W; g - }' a4 B% Q, [0 S% K
- /**- N' o' h) A( W1 f
- * 物资规格
9 }/ a/ ` U8 e& Z - * @return
9 K" `7 {4 N7 \# a - */( j% V4 t7 a( H: r- C: ^# I
- public String getSpec() {
3 J* X7 ]3 N( L5 H) ]) W - return spec;
0 G7 G: K4 [& H5 C - }
2 g6 o' [9 j& D; D - /**
0 Z0 C, ^5 D! U5 M. v - * 物资规格6 W# w8 P; T4 a2 C$ n% Q M
- * @return! E1 @' f- ~) s: o) j
- */9 m0 q/ \/ g, r
- public void setSpec(String spec) {
5 }/ ^: a l9 N* K, ^ - this.spec = spec;
4 G! B6 D/ ]4 O - }; e2 \1 f8 x, _
- /*** Y+ b# v* r' v
- * 物资数量
( u) C% V/ Q6 S4 D8 F* _' q! J - * @return8 U+ v( W$ j! R+ P3 @
- */( n! O. {9 b& `3 [& R; `
- public int getNumber() {; }$ R, y- a w+ V
- return number;
9 h: ~7 y$ C& D0 r* Q) S1 H/ |4 d - }: [4 U4 s5 v( C% ]8 }" n( J& s
- /**' W8 J5 \' @& L V
- * 物资数量4 v1 g- m# M! z! _5 I
- * @return9 B6 |6 g2 b1 b: P2 K e) ]
- */
# J D: n0 U3 L8 U# U& v - public void setNumber(int number) {
6 k1 p# o% k+ q; T0 y ` - this.number = number;+ i+ g e8 X4 ]+ I4 Y9 r4 @
- }' m9 C+ R5 _" ^& ]8 X* ?
- /**
3 ]8 w1 T% a3 |2 n+ M3 ^ - * 物资单价8 V' h6 c6 F5 X& F
- * @return; [' J! U4 C0 Y0 |& j2 @! \
- */
7 }3 d, [, D. p - public int getPrice() {0 O6 i9 }: ?$ X- w+ ^6 }
- return price;
) ^: ? p( A% f! q. z' C1 r - }: b- R$ S% [; w, y8 ^7 P' ]( `
- /**
9 k( H# ^; s- p& k5 Q, U$ r5 t, M - * 物资单价
/ k% m) o, y/ n. V' D - * @return
2 r2 \% s/ a9 C - */
$ M; j9 N# ?2 G, h p2 }: h# X - public void setPrice(int price) {) x0 H. y5 v! y; D
- this.price = price;
0 o5 s) q! Y$ a& y) ~0 A: q8 ~" b$ { - }) n5 u9 S' d1 L! N" r# V
- /**6 @) S% k2 ?; ^7 w0 }
- * 生产厂商
! X! g+ O1 B0 C8 y+ W: K - * @return
; o* R% p# q' y" ] h0 \( l) ~- c' n' d - */
: s# C* H" U- t' b - public String getProducer() {6 s9 q* v/ B0 l
- return producer;- H1 H8 c7 y4 E/ `
- }: j1 ]8 H8 ]3 N% l1 R* v
- /**
3 Z ~, K9 e0 j# }! | - * 生产厂商
% ]5 K$ Q r I/ ` - * @return! k! L ?# c7 S7 B, W8 p- m
- */
0 n4 p# }& M* c0 A" P q9 ? - public void setProducer(String producer) {; L9 L8 [) _: L
- this.producer = producer;
+ c1 w- P: O& R/ N - }# V# X9 Z; |- j9 s
- . N. t+ Y; C) R& q% q l" X B/ A
- }
$ J" a$ ^/ L" w9 x9 ?/ u
复制代码- package s2.jsp.zhangxiao.daoImpl;7 Y; J( o9 ?7 `/ p4 O8 ^
- import java.sql.PreparedStatement;
8 H( p) `. L1 k& b4 j - import java.sql.ResultSet;
0 z' R3 H: O6 D" L5 ^ - import java.sql.Connection;
8 B% S% i- f# p7 c" E - import java.sql.SQLException;, Z9 Q* ?1 a( f5 P; ~
- import java.util.ArrayList;
7 X8 S" g# J0 Q8 @/ M' N0 z. @ - import java.util.List;1 S. o' {- D( Z3 b, Y
- + {9 ]: A+ y y+ j
- import s2.jsp.zhangxiao.dao.MaterialsBase;7 E2 D5 L1 g/ y5 t
- import s2.jsp.zhangxiao.dao.MaterialsDao;4 r' S U4 Y" v$ \! M2 \3 e
- import s2.jsp.zhangxiao.entity.Materials;
' y' L) m; Q1 P% w - /**6 \$ q7 Q( q3 M0 d4 C5 p
- * 实现接口的方法
# u, t m9 ^+ x - * @author Administrator
+ T4 Z3 y# R, K - *
# s4 t6 I& k) y$ Q+ W - */
8 j* ^; X& X) X+ H - public class MaterDaoImpl implements MaterialsDao {
9 h0 R' s9 g/ L$ ~ - /**6 h9 h6 Z" f2 f N
- * 查询全部
. {. \1 x; l6 B6 T" Q - */" I; \9 {: A7 N, F
- public List materialsAll() {: I# q) x* M0 z6 d0 E
- List list=new ArrayList();
+ x2 F) ]. d* k" j, H) S - Connection con=null;
6 J& f: z$ d( q! w - PreparedStatement past=null;
% E4 g8 R* g7 @) O - ResultSet rs=null;
7 c( u( e4 d$ x" l/ U+ ^6 Z7 P0 E - //sql 语句& ` w$ k8 w' O2 m+ @
- String sql="select * from materials";3 _# H6 ~0 g9 s* `% l
- //连接数据库
* B/ O, q$ V" x9 O; [ U - con=MaterialsBase.GetConnection();, _; }* y5 V/ ?5 f6 H! u
- Materials materials=new Materials();
3 D% F$ l2 B7 v+ R - try {
5 ?" l) v* k# D# ` - //传入sql
( n+ V) @! d" S+ U3 q6 m- ^ - past=con.prepareStatement(sql);6 s) r% j! l l; e- t8 ?6 W2 u
- //执行方法6 t6 H/ F4 K( }3 u% h# h( u+ C
- rs=past.executeQuery();
# R$ p+ _" Z& ]7 S' D4 E - //循环读取数据" T0 o0 T/ L1 x7 O
- while(rs.next()){
5 C2 y0 S( g6 g1 k5 Y9 C6 J - // id, name, spec, number, price, producer
" a. y% a6 D4 ?$ u; M" y% u - //编号
0 F) |2 t3 |, l- |$ i! i& \ - // int id=rs.getInt("id");
5 p( e, x6 n a) ~* i2 B: d: G2 I - // //名称0 P+ S1 B d% l+ E) W
- // String name=rs.getString("name");* Q, h& k* G2 {# F
- // //规格
8 ^" u* u+ j1 w: t9 ?" R+ G - // String spec=rs.getString("spec");
* u3 ~% x2 O! Z - // //数量
. ~ x1 C. f: R' W - // int number=rs.getInt("number");
! e p" ^& b: Q3 C$ C - // //单价+ g2 ?2 e/ s4 k4 y' T2 b
- // int price=rs.getInt("price");0 O9 S8 u* z; z, V
- // //生产厂商
" L3 c+ c8 E8 h/ G1 g - // String producer=rs.getString("producer");5 L9 R- e' f4 H/ P
- // //添加到构造函数" z% p7 x* \6 z* ?
- // Materials materials=new Materials(id, name, spec, number, price, producer);; b1 k/ E4 Q1 K5 O: S8 [/ u
- // //添加到list 集合
, i7 f6 }) P5 Y7 J0 I7 s! h( f - materials.setId(rs.getInt("id"));* x% Z$ Y- L: E) h+ x" T7 X4 ]
- materials.setName(rs.getString("name"));
% B( C5 h/ W* B) ` - materials.setPrice(rs.getInt("price"));
6 S2 H: }: _8 { H - materials.setNumber(rs.getInt("number"));
) |5 G, u) o8 ] - materials.setSpec(rs.getString("spec"));
! M5 x& q+ X2 O# l! C - list.add(materials);
. e; F) P( j# H% F4 s q - }
9 y h) }& f* ~! n6 V - } catch (SQLException e) {
5 H7 I; o" V4 }( Y) S6 {+ S: V - System.out.println("materialsAll 方法出现异常");2 P- F; [* X( f8 [' E- l
- e.printStackTrace();' G. K3 ~$ o$ n
- }
& e8 a- d% N! { -
: u2 M8 n5 }8 s; E9 }( u - return list;
# j( J/ `9 o; @( H; ^" q$ r/ T - }# K# I; k9 q' V) h
- ) V0 b6 }# S$ y* U( K: L8 h
- /**
7 d. F6 E1 V. [% N - * 根据id 查找物资信息
) g! e7 F8 C, I* O9 I - */
) Q0 {$ p* s- v- Q+ { - public Materials materialsID(int id) {
9 l) Z% A" B% P: ^: }. C - //声明物资类
9 q1 J1 `# f4 G1 J - Materials materials=null;
* @; s, t. K9 e( g8 U, w2 u" _ - Connection con=null;$ n" C. ~& p) u
- PreparedStatement past=null;. u; u5 E* k) q
- ResultSet rs=null;
2 O) a9 I; A0 z7 B1 H - //sql 语句
; ?+ A* R' J+ H' I) A9 z9 ~# R - String sql="select * from materials where id="+id;
5 X! H6 A# B+ ^% {# u, F9 ] - //数据库 连接
0 ]6 q M/ D# `* w - con=MaterialsBase.GetConnection(); `0 }8 t3 l! o( O- y% J
- try {& n9 ]6 C% O0 A' _; J
- //传入sql4 O0 A4 }( L1 N& c" |! ?1 k
- past=con.prepareStatement(sql);* ^ `" C# Z0 c0 e3 Y
- //执行方法/9 f0 e% ~( p- J( \$ f9 {
- rs=past.executeQuery(); u ^/ b6 g; u7 p* Z
- materials=new Materials();
0 U1 k f- u2 g0 R! m - //读取数据3 ?- S. k( o) u8 k: s" U
- if (rs.next()) {+ N9 D8 [& k' k$ M
- //名称- [2 V3 C$ g1 h
- materials.setName(rs.getString("name"));
+ Y3 h! ]$ L6 Q! c9 g5 l" s# t - //规格8 Y$ N8 G2 b( j# r6 f) i
- materials.setSpec(rs.getString("spec"));: n7 A8 S: O( a& C s; {; t" s
- //数量8 ~5 a/ t! L6 Q% O
- materials.setNumber(rs.getInt("number"));" A' R" d5 D9 N0 p
- //单价2 t# U" P j3 j# ?2 v
- materials.setPrice(rs.getInt("price")); @4 ?$ G8 s- d$ k( e B
- //生产厂商
9 }# Z- Y: H2 A- j( V - materials.setProducer(rs.getString("producer"));
& w2 h3 w% M. i' S6 D5 X& A+ M - }
B3 n( b* C7 c8 j - } catch (SQLException e) {
' G; S1 o& j A! i( G3 W5 Z - System.out.println("materialsID 方法异常");
/ n c7 o- Q, P2 J8 ?7 L7 c' J - e.printStackTrace();* J7 U# N$ u; K* q2 q8 P
- }finally1 ], _. _" b! X
- {* b/ F! L; C2 z6 Q7 Z
- MaterialsBase.CloseAll(con, past, rs);0 y1 U3 z/ A; J# m
- }
: |& |! e, n, g3 ?, _: ]5 _$ ^ - return materials;
6 t' ?7 P- G+ G: ] - }
; O" T T; H- [& [ - /**
, g8 Y- ]9 g* O0 x - * 更新物资信息# x. P! E$ e$ a3 g x; b
- */
! G M. D7 X: J. y5 C% z' p/ { - public int materialsUpdate(Materials materid) {3 Z- k9 _. Q4 S7 Y4 s9 u4 C( Q
- //sql 语句0 m# E& A3 o/ ^( n/ {' g& M
- String sql="update materials set number=?,price=? where id=?";+ F+ \+ f' l, u. k7 G
- //4 b! S# {" c0 ]6 X+ H$ e
- String[]getValues={materid.getNumber()+"",materid.getPrice()+"",materid.getId()+""};& Z& Z4 r3 x9 o0 p$ D
- return MaterialsBase.ExecutUpdate(sql, getValues);. f5 _0 U& w5 X- @
- }
# n6 x. |/ Y0 u$ y) Z4 o
" R3 w& h9 D2 o' f _* T: b, x" Q- }
* L- N4 {0 A$ P1 L6 x( J+ L
复制代码
( n. i2 B+ J* g6 O# I' N, C5 r代码案例下载:点击下载
! K4 J3 k" U. @& N, }
. ~1 q0 a- ~5 ^& u( i# p |
|