该用户从未签到
|
- package s2.jsp.zhangxiao.entity;; Y0 a/ a: b ~ ?1 x9 v0 ?
- /**
$ w; Z7 o" `* ~/ y - * 物资实体类$ U0 J1 f8 |4 C" j, ?$ w9 j
- * @author Administrator4 r1 u; ~, k0 r, _' r3 s' [& c( k
- *
; G' i+ k/ ]) G) n - */
2 Z, E9 x% s; v& s. `" b - public class Materials {! O* N; z- u4 Q' K& T6 w( H
- // id, name, spec, number, price, producer
- M5 D$ |. ?) R! j; D" I! m) l - private int id;
# C' n/ y0 s E$ L5 ] - private String name;
+ W+ l4 V2 ?1 A& E# X! \ - private String spec;9 n7 }& A3 U1 ^4 A( r, |& G
- private int number;
4 X# _4 T8 f) ~; d1 ^3 u5 o - private int price;0 P: S' Z, D: M5 V W" G+ f( F& u8 r
- private String producer;
- ~3 }8 i6 D3 T. k% z8 P - /**
+ R. Q' t: G& z% I2 w' w/ D. I - * 有参构造函数) ~% I6 S9 e; x3 {4 e( V: n
- * @param id
. h8 f5 }7 ]# f$ o - * @param name
* [0 Y7 F+ x$ a7 f - * @param spec% j. @( }: X8 l5 q
- * @param number$ ]$ ]" r- Q0 e! [2 n+ @1 ]7 H
- * @param price
' d/ G7 _/ M$ |, k# m% T# I! W - * @param producer
$ K# m. S2 K- H" ~ - */
$ `! I5 n7 W+ s& c: m9 G0 y - public Materials(int id, String name,String spec ,int number,int price,
2 H( m4 Z% x' E6 d5 S - String producer ) {
- n4 v, k* ~9 w" d - super();
1 Q' Q- ^* U) n3 E+ @& O: O2 j - this.id = id;
* L( L+ E5 i2 k. V - this.name = name;
0 ^5 I' H, ~. [( X' ~: }3 I - this.number = number;
5 w" t. N6 \% \ - this.price = price;" m* I. l, z/ B' K+ |1 x+ l
- this.producer = producer;
7 O: @# w6 ]7 i9 ] @( K# c - this.spec = spec;! \% o" u7 e/ Q$ I3 o9 K
- }
# @0 U% u: o' }+ h: i6 t1 E, R$ [# E - /**
% @8 D- w: t* a& K' Z - * 无参构造函数
8 J k4 [$ Y" R, S& E* U0 X! c - */
3 f e5 m# r: V - public Materials() {
, ?, T9 J: b( l& }7 c - super();
( k2 R4 G2 s4 _ - }* x7 w- q% L3 e- c
- /**- q0 a9 P" d; T! a
- * 编号
) C' C: w. _& _. U - * @return
# B$ h; ~/ D. N7 ?: n1 z5 v9 [ | - */$ H: M4 m' Q( K0 S
- public int getId() {
- }" |; s7 j. l" B5 b - return id;
5 y8 H' ?2 u5 j! _9 m - }
7 N# S" e8 d$ }8 F$ q2 U! O - /**; l. F' g9 w8 V; h9 m, {* o
- * 编号
% W7 X- D% z8 y. }4 N - * @return" K2 W5 B2 v. H
- */ b& f/ W" B0 h
- public void setId(int id) {, t/ ?) K, z. [8 M" F; |9 {
- this.id = id;
+ U6 k* U6 ^% _ - }" k7 V) c# D8 m0 _% E1 [# G
- /**' m/ c9 \1 d, K1 [4 F
- * 物资名称! U7 O. ~" H# Y/ ^$ M5 r
- * @return
! R3 B; m0 B4 Q- [# r2 ] - */9 ?& z( Y- |% l4 F
- public String getName() {+ F0 f+ D6 \: E' Q3 ^
- return name;
+ {& F4 h8 ], ` - }
0 A, C1 z! p( v. a2 E - /**$ g6 [, P+ j' X" _/ C
- * 物资名称
- h# f n p9 }. ~) @! v; @" \ - * @return1 o7 p. q- E4 g" e4 E" |: K
- */
( X7 u4 j, v2 C: o) _6 A' h - public void setName(String name) {0 W, `# ^: u7 ^6 J9 i- ^& l0 u( } B
- this.name = name;
' h+ y' w. X9 @- t. B - }
6 w0 p0 n. V0 h( _: l2 G - /**
. | c0 D- ~5 l - * 物资规格1 T. P2 f; w; u0 b4 h
- * @return1 v+ F$ \! @8 Z9 x# V7 B# ]
- */' n. Z! q0 s! [! c
- public String getSpec() {$ w# }+ y6 t) p: ^5 M
- return spec;- [/ y" a. Q, R3 Q& s
- }3 r8 Q3 P* @! H. h9 e
- /**8 X+ y4 J6 e; L4 q) _# N' b4 Z
- * 物资规格
' O- u9 X/ t/ W; q" b - * @return
, n# s. D% Y1 g, d" t - */
( t( `8 Q2 ~6 s) D% k3 F - public void setSpec(String spec) {% X, W' q/ r% o
- this.spec = spec;
& U( a5 h+ h1 q. @ - }3 j5 Z5 B0 }& p/ g' U
- /**
- r( v2 C B; D$ P$ a - * 物资数量
' [: m+ S* L# a' s0 p: w$ T/ W - * @return" u2 ~+ `" E% r& J
- */
9 R3 F# W- u% p: `, `* {6 Z7 F - public int getNumber() {: E, ~" _6 M3 g7 n
- return number;
8 H$ s; R4 |9 h+ l6 g3 G" @ - }6 x1 W4 n" g. _7 n% _$ w1 T
- /**
, W; J1 Y5 \0 i4 }( T( D - * 物资数量2 j4 z6 u) r, c8 E8 e
- * @return
+ X$ A$ V7 u( o* E" {+ a$ P - */
/ D# X' Q) y D4 a8 p0 B - public void setNumber(int number) {
; a8 j& ^& |% w* s' M - this.number = number;! r1 v+ p' [; F& e. W( t
- }
4 O+ |" m8 h; d4 U: b& h- W - /**9 U' ~4 f3 {& t9 a+ G) [
- * 物资单价 r; ?9 G) ^4 [# H
- * @return, R4 p( f9 U( a+ L) |1 }
- */# z# @. y$ j, A4 W1 F+ t! t
- public int getPrice() {/ L* {% p. v& C ]
- return price;
( ]8 E9 U' k3 X, F$ C - }
% C$ O; }4 Z/ p9 T - /**5 T) k# Q m: Z$ ^$ X1 t
- * 物资单价! t2 v9 D4 _; }# ~* j! j0 B
- * @return
2 T" `0 o5 `' i - */1 @% r5 O' H7 C- I( t) r4 X, \
- public void setPrice(int price) {1 Q$ [+ x# W! b2 j1 z
- this.price = price;% u! i9 m! n" k4 ~* z2 _' z
- }1 Y/ Q$ x/ ?5 Q$ N/ B' q
- /**
+ R3 g3 S& V7 @, E" ?2 | - * 生产厂商+ J4 o6 y' L4 `6 K# n6 X
- * @return
- U m7 s8 n9 J. M' h! b! A* | - */; \/ b0 j' ?# [7 g3 L/ W; q3 t @
- public String getProducer() {( q4 ]5 M! e4 Y% _. h
- return producer;/ ^- H1 Z! }+ v( L0 Q, a6 p5 K2 f
- }: y! P& i/ W: ?0 a
- /**
( X7 A& R: k; v1 S - * 生产厂商; W4 j; e: Y0 N1 j- w( Q( d
- * @return. Z% W/ A" o& o
- */
6 o; r- f& m% y0 G - public void setProducer(String producer) {. O) \. Q c! n1 a: q+ X9 {( e
- this.producer = producer;
5 y9 l7 K8 K, ?, r( R; T y: a - }2 P7 C! @' D: r. M$ f
- ; {- `: h8 M0 d/ ~( [# c6 L0 Q! @2 e
- }1 d& m$ u) b% L# P9 f
复制代码- package s2.jsp.zhangxiao.daoImpl;
# @# ^5 h' B' H' K) O6 p% I' X - import java.sql.PreparedStatement;6 v2 @. U$ X2 J6 m6 z
- import java.sql.ResultSet;
& F' ]9 H+ s% }1 P" Q) S+ O8 O - import java.sql.Connection;! r: y/ }/ U' B* p% C; P% C7 G
- import java.sql.SQLException;
3 i/ ?& O# E3 R7 j+ t" b - import java.util.ArrayList;
2 U- q8 d: Y/ q, y l4 w% g& C - import java.util.List;* w! @% ?0 M$ F" S
: H) x/ }# _+ H% m& J' k$ }; k- import s2.jsp.zhangxiao.dao.MaterialsBase;
; O* V5 J: I1 w - import s2.jsp.zhangxiao.dao.MaterialsDao;
" L4 y d: F: U; n7 f& R! X - import s2.jsp.zhangxiao.entity.Materials;! @$ ^% o3 A0 n, E! l
- /**, w5 F& [3 P: ?- e" b0 Q% {
- * 实现接口的方法! V5 ]; A, V5 {, O& V5 p0 s G& j
- * @author Administrator5 ^9 o4 D# Q( |0 j
- *
- B" `1 D* o9 _* i5 j8 F - */! e6 q6 g, b) U- g/ y( h
- public class MaterDaoImpl implements MaterialsDao {' q! w S. f9 n) ?/ x! U# e
- /**+ X$ p# @5 P7 e+ p3 d1 n7 [9 t2 Z
- * 查询全部; Q1 D- h. q& N: ]0 m# |; ]# a) i
- */
" R. o" E5 ^/ c0 m. g5 a, G8 ~5 N - public List materialsAll() {7 S' g9 l4 I" \. _4 b
- List list=new ArrayList();
5 U- N- ?# G1 g1 o) s - Connection con=null;
% |$ C2 m* J) u# a& l; G! g5 U - PreparedStatement past=null;
0 S% H5 T, _! p1 u B1 O0 k - ResultSet rs=null;# ^8 Y6 }! V" r3 d4 o
- //sql 语句
5 ~2 P! f4 T% V9 \9 `& T: {& `/ T - String sql="select * from materials";
2 P6 I" \! i% D1 b: x - //连接数据库
) ?1 X- h" ?( L( e4 _7 \ - con=MaterialsBase.GetConnection();
+ ~7 N, @0 Y0 F) z9 C - Materials materials=new Materials();
" ^; {, y9 w1 c9 Z; I - try {
: G' X1 W* p B' _ - //传入sql 9 R# e; s6 L$ O4 [; `, |/ c% m
- past=con.prepareStatement(sql);# w4 c ]5 D+ S; c$ { _- [- z' E
- //执行方法
- ~+ N( Z5 C9 @3 f( T - rs=past.executeQuery();% X$ _# W* Z. k0 z, Z) g
- //循环读取数据
. i4 P: a5 p, `( h- b# f" j - while(rs.next()){
' m$ W, p% m: z9 K - // id, name, spec, number, price, producer( J( Q4 \ q" b. L& ?8 |, M- o
- //编号
! H# m& K3 \7 K- t4 \( H - // int id=rs.getInt("id");2 s/ W% h# [$ d! n
- // //名称
) D+ Q; E$ u+ f: q0 N& O9 I - // String name=rs.getString("name");/ ]3 j& [8 w) t" b6 p8 w
- // //规格3 X0 }8 N4 S' j0 C
- // String spec=rs.getString("spec");
2 u0 E2 v7 k6 y& v" ?5 I9 c3 }5 D - // //数量/ ~$ `- O. h& o5 N5 m
- // int number=rs.getInt("number");9 p! s% L% y( A/ ]8 b' @2 [
- // //单价4 J( _ v# P6 h6 B
- // int price=rs.getInt("price");
. a2 c3 q; Z9 {1 R3 O4 b* @) @ - // //生产厂商
, L4 `$ C: v& ^ - // String producer=rs.getString("producer");# q- H9 ^; X) p9 D e* ^6 Q2 l
- // //添加到构造函数
" H S' e9 l9 x. u* c+ g* T - // Materials materials=new Materials(id, name, spec, number, price, producer);2 X# a6 Q: C6 N' |# O+ u
- // //添加到list 集合
/ Q4 |0 r$ \# D$ `+ ~( Y - materials.setId(rs.getInt("id"));$ G. \1 s5 @0 D
- materials.setName(rs.getString("name"));
% R, j; [; S3 T/ Z7 {" f5 | - materials.setPrice(rs.getInt("price"));
* I( U" l! ]% t a- y3 M - materials.setNumber(rs.getInt("number"));& R' ]. C% }5 L1 p( q' M& i( T) J
- materials.setSpec(rs.getString("spec"));) X K! m4 Q0 b: [
- list.add(materials);" w* a2 }! I# M1 t
- }
$ b# X! @5 ]4 S( e$ {6 J - } catch (SQLException e) {0 c( s7 y8 D% G& H5 \
- System.out.println("materialsAll 方法出现异常");, o8 e) {( _7 C) h- G( f9 Q
- e.printStackTrace();4 \: j6 K6 U) E, Z% V3 ?
- }
) [0 m4 _7 P; j% C -
1 i* N1 ?3 }0 ^. | - return list;
# Y* L8 x# j ] - }- G. I* z4 U9 U! n$ m$ y2 W
-
3 m( U4 v ?' ~& m+ J; p8 v( {1 g - /**
+ b2 [" Q# k8 L7 _1 l - * 根据id 查找物资信息' n; g4 ]* e" J! ^+ k1 ]2 Q' v7 S
- */' d$ S( f, v9 l6 q
- public Materials materialsID(int id) {. v6 |4 W, \5 P' _- [5 m( ~
- //声明物资类
% j f8 j) t" l8 m - Materials materials=null;
9 M3 i$ f% a2 M& j1 l - Connection con=null;
$ Q1 o; F2 c8 h4 d) d - PreparedStatement past=null;$ z9 [: C* U9 y" q: d3 H
- ResultSet rs=null;
: H# w: z1 X& M - //sql 语句
2 A+ r: S. S+ g0 K G2 F' n - String sql="select * from materials where id="+id;3 G! }6 x: M: C: ]8 N" _7 x; C8 V
- //数据库 连接
& y) [* ~3 p9 j! W1 Q( p& N, u) [ - con=MaterialsBase.GetConnection();
! Q: q% C: |4 V' c" d5 F - try {8 k! R2 M' t; g, H
- //传入sql
- q6 v" e" T3 { l4 ~7 k' I - past=con.prepareStatement(sql);
7 E* [7 ]' g! J6 f: W' g - //执行方法/% v/ j/ q9 i! d+ c, i7 Q
- rs=past.executeQuery();
" m2 I$ @1 \' Q+ Z$ Y - materials=new Materials();
5 ?, F8 A- q. m# c, c' |" d - //读取数据
; F4 I# E: n6 V3 g5 y - if (rs.next()) {# t9 G9 q, |3 }
- //名称' M. O3 Z- O" [5 k; m" F
- materials.setName(rs.getString("name"));
5 [3 ?' p- P7 v U/ L0 w) A0 ^( x - //规格; W7 k. L8 U0 Q7 v+ B; e8 K
- materials.setSpec(rs.getString("spec"));
+ V' l1 @- p& g! A& U( I - //数量
- h$ G w& y6 n: n0 o* w: x - materials.setNumber(rs.getInt("number"));* r; ?* u% @7 ]- t# u/ F% _% b
- //单价3 m( d" _, n# Q! P U% Y! b5 n
- materials.setPrice(rs.getInt("price"));4 z: Z( T' E% r: n" ?( f) g
- //生产厂商
7 T# Z* T, |. L T% ~0 s - materials.setProducer(rs.getString("producer"));2 d0 z( z4 G; w5 j7 e+ B
- }" r7 H3 |, N2 Q: u5 A
- } catch (SQLException e) {9 k3 r2 ?1 |( W _9 T
- System.out.println("materialsID 方法异常");
& m$ o C0 B7 j7 t: t2 T9 P: k% g# k! S - e.printStackTrace();
2 i3 ]0 S' M9 v* c& M. M - }finally8 k9 A- v' ?& p7 g
- {+ o# r. _# X$ b/ J( {
- MaterialsBase.CloseAll(con, past, rs);* ^$ W1 D. a4 ^* _5 q% @& d, U
- }( p3 c0 _% L5 e! H+ @% q
- return materials;
2 y7 B2 C D; a8 C% N - }- _8 O0 |5 J. z- t O
- /**
2 I& ^5 S7 C& C; ]2 o - * 更新物资信息
0 J% v' N/ a4 |: _( W - */: g7 R. L: }' {# c9 O
- public int materialsUpdate(Materials materid) {. Y) ~1 Y' c7 Q5 v
- //sql 语句; N$ S- M& M$ ?3 B
- String sql="update materials set number=?,price=? where id=?";0 y, Z3 U1 e. ]: T
- //% ~8 R5 n: u- v* K. b
- String[]getValues={materid.getNumber()+"",materid.getPrice()+"",materid.getId()+""};3 u9 ?4 Q# B1 S& B8 L2 b
- return MaterialsBase.ExecutUpdate(sql, getValues);
; X/ I f; q) U) [0 h& J6 T9 z - }4 K# G; A; \9 p8 Y
- & P8 r2 k8 ?0 d4 J/ Q$ ?
- }
8 I* K$ B+ l1 E* g7 y2 T" e/ ?
复制代码
- i; E S1 g% T6 u/ t代码案例下载:点击下载
8 F# R+ h; p) I7 E; M# r& ?! K% n- R! O
' c; Y Y, r0 U, F# `" w0 t |
|