该用户从未签到
|
- package s2.jsp.zhangxiao.entity;
4 @. M U/ I1 @( ~; X9 ? u - /**" f5 ]* V' C0 k
- * 物资实体类
- J$ ?+ o! R& w/ T2 m - * @author Administrator
H0 ]2 M% j7 S! z - *
1 K: _* [2 F5 q7 a& H# `: w" m8 R - */* z+ @( ?: U* d2 V, q; W
- public class Materials {
! _" V. O+ A5 Z! W0 x - // id, name, spec, number, price, producer
8 d5 c* ^* T- y* H3 X - private int id;
$ R/ \ \, C. _5 I* p, T - private String name;
9 q" F, a# q+ u$ ^ - private String spec; R+ t; V' b) j
- private int number;' [% [4 e; y9 B1 a* n8 Q$ H
- private int price;
1 N3 T& O3 U! M! e; f; G - private String producer;5 _5 s3 l8 f2 l) z! `+ |5 B& v
- /**- U! l# \3 j8 h; V7 {
- * 有参构造函数
' W+ R* n- ~9 J# m - * @param id
) a4 i# @/ p! i1 Y9 N - * @param name5 P* H7 \" f; h: W9 T) i$ y
- * @param spec' Q; h" O- A5 d: o( k- d9 I9 L
- * @param number. t1 v7 p1 u# J; n
- * @param price
: y* f# ?) ^3 L) X) N - * @param producer
8 `& c" M2 ^& Q4 u- E - */
' \& V% u4 b* O4 ? - public Materials(int id, String name,String spec ,int number,int price, " h5 R; o" o3 v
- String producer ) {0 v4 ^! `% w8 i5 {& v! A7 j
- super();9 E* X' R7 c7 |5 U' q* |4 \$ V
- this.id = id;
; |$ M% M9 {4 I* s+ K. P2 i - this.name = name;, `: a& M$ q' O
- this.number = number;
8 w& X# h6 @, s - this.price = price;: l; G) H( C$ X9 ~! ], c; Q1 E0 s" m
- this.producer = producer;
* R9 t- _& W' z$ U, p3 x - this.spec = spec;8 T% p5 F7 [* \; r8 V: ]* n
- }0 F3 d5 Q, C2 I* j2 `" Y
- /**
5 }+ ~& w: N3 c* K2 `. ]5 t - * 无参构造函数2 G0 Y/ o) R8 g! P( h. x
- */4 [: p0 J2 p" S) S4 N. V, g
- public Materials() {3 Y$ t$ Y P* P7 {+ l
- super();% n4 C$ G: U! H% ~1 m: |# R+ f5 J
- } ~$ T4 ~, F$ D s/ l# p
- /**5 f7 \1 @/ E. k" x' M
- * 编号
; {. B( P0 ~2 R - * @return) [; t( a* a, \5 j9 e, [" }9 C
- */
9 X# h; |9 G5 b! A8 ^- Q; A5 H2 \ - public int getId() {
: i- Q' o# s+ m8 O, T/ ~ - return id;( i% c9 c! n# J U5 O% f6 C: G+ d, U
- }
' p% q" I2 u" Y: ]8 w - /**% N3 @7 t) ?1 m( n/ F
- * 编号
. [4 L; [1 z1 b# B7 y - * @return
e! M0 Z2 S9 U$ o( O6 d! y, u - */
4 @: A* ~, {4 O- i9 i. E* d - public void setId(int id) {( j. v+ \! l% @( R+ A) [& D
- this.id = id;
' M4 O8 ]$ |) m) ?8 z2 }' i - }; m" P( e, E' F' ^& M% x5 ?, j( Z
- /**7 p' z, P4 G5 \! E2 Z
- * 物资名称" I) e+ k( s7 d
- * @return
. w5 b$ K# ]4 d - */3 i- e" p' P \! A' O
- public String getName() {
. [1 S9 v! ~, q! S6 E' R+ H - return name;
: h9 w% G$ |; [/ r" r; L6 a6 w% Z - }
9 @1 @# Z `. j* S- [, O0 d6 r - /**
x: C5 \& Z \- L - * 物资名称
' U$ v) J1 E! `! c a0 c - * @return; r& C, J% V" s4 ^
- */+ l' L% ^# q7 W# K+ J" Q9 y
- public void setName(String name) {
p8 E8 V/ S3 J( d: x2 H7 @ - this.name = name;/ ^$ r/ t5 v& [$ H0 g3 D
- }
4 L; V9 q% l: A8 z - /**
. `: t3 q! B( ~3 f4 J - * 物资规格# w# p+ U4 w( s3 |/ L
- * @return
6 x6 ?4 U) e7 v3 g7 T( W( x& O - */
) ]3 v9 G6 K! I - public String getSpec() {
% ]! u! e4 E k/ l; d h8 d - return spec;" x% W( i+ ~6 h% z$ Q0 \0 ~0 G
- }
9 r) f/ x& T; l - /**
$ o& R+ f& h+ R9 G( t - * 物资规格
- A, v7 v& w' T8 H- u - * @return+ R! i& f1 g5 [/ o# C
- */9 Y% z/ {# a6 r6 ]$ `
- public void setSpec(String spec) {% r" ^% }0 _7 o9 R
- this.spec = spec;
+ K) E- J$ q) b8 n* b - }, ^6 w- l U0 r
- /**6 q" O Z) Q6 k2 w( _
- * 物资数量) p6 e, T5 x {
- * @return8 Z, k6 b# y. r U. G
- */* K& V- O1 r: A
- public int getNumber() {
" _+ O8 r0 a3 A3 S4 U8 ?% n - return number;6 p K9 o. e) y7 E; W: V E7 y
- }
. `3 y( Z* F1 F9 d6 T5 `' Z - /**
$ X8 R' v! s& u/ R( }: O! F7 M - * 物资数量2 I& S2 y: A) N1 ~2 J
- * @return5 F7 d. P9 k* T; l$ @
- */# u/ s9 H' |" I L7 o1 _
- public void setNumber(int number) {
$ B: u( x$ w n5 l - this.number = number;
( e z1 i( w' z7 y - }
* t2 Z: I$ l3 J2 E - /**
- |; Z$ v( n8 x$ a - * 物资单价
/ X" S4 Y9 n3 B) k8 N% ~ - * @return* k; e9 n5 a, ]3 r+ H4 B
- */% D2 p! I% o! ~+ ]& J% _
- public int getPrice() {
0 n( X; P- V( S - return price;
0 i% @9 c" r' R6 s0 U& W - }3 S& f( L3 D; \* {
- /**
6 s$ g3 |; Z8 f/ a; c3 q - * 物资单价
' Q8 ?/ \4 y: @5 S - * @return$ g/ t3 u3 J& K/ N6 l
- */1 {1 a- l9 x J7 ~9 Q- b+ X
- public void setPrice(int price) {- Q/ n; O: ]! R" E" t6 Z
- this.price = price;
+ x) n) d6 e. Z! y% O" V7 z - }
6 Y3 ?& S5 ~$ F! b- E* u" Z - /**5 O7 Q1 u. C8 l/ l' w; `
- * 生产厂商
9 r5 @! ^7 e8 P" t" I8 v& p - * @return
7 B; x0 b2 C( [+ w( \4 z9 d - */+ a1 E0 e j2 V$ l
- public String getProducer() {
+ N9 u2 \$ z# _0 d; w+ q - return producer;/ e8 s, B) x' U, `+ X. W# ]2 t
- }, h! y+ `0 [2 m$ h
- /**, W# ^9 G( M! U: [: e* S: ?
- * 生产厂商+ k2 ]# f! F3 r
- * @return
* {1 l" r% u9 A - */: v9 ~ H; F L, f
- public void setProducer(String producer) {
/ o8 l+ D5 o7 ~9 c2 ]+ k - this.producer = producer;
6 w( G8 t& S" F% _ - }: y0 d1 F/ T9 Q9 _" S
-
5 e0 R8 P; r) Q6 s2 s - }3 o) J% i5 h* H/ `+ A/ t
复制代码- package s2.jsp.zhangxiao.daoImpl;
) o5 `; v5 X5 u+ \+ v - import java.sql.PreparedStatement;
& Y* Z$ Y {- t7 g# \' ~ - import java.sql.ResultSet;
1 X5 r: S3 U1 s' O' L: n - import java.sql.Connection;% b+ c3 y& k" t p
- import java.sql.SQLException;
: B( s$ x& B' ~# ~ - import java.util.ArrayList;
" }( g1 y( P; n5 f4 K% T - import java.util.List;
# c: a, g. q; I: l
* W6 f4 X* Y+ q, @- l) |- import s2.jsp.zhangxiao.dao.MaterialsBase;; C/ b# q0 _1 p7 e$ [* l/ z) B
- import s2.jsp.zhangxiao.dao.MaterialsDao;
/ k9 `6 S) F6 {5 n - import s2.jsp.zhangxiao.entity.Materials;
; ^8 a( U4 g# W# @" ]# {' [6 l - /**
6 P9 y% i1 m$ B - * 实现接口的方法; r/ \9 {$ I) U1 D3 c# G, E7 s
- * @author Administrator
4 X+ O! s/ l4 G" ^: T+ r - *4 q. f& N- i) \1 |8 x
- */
4 V$ d; \4 G) s% O9 u - public class MaterDaoImpl implements MaterialsDao {6 {- l" B/ x1 m; j' q% {
- /**6 Q0 g9 G% w/ u7 [' d1 T5 S3 j
- * 查询全部2 L! Q; U2 \7 v6 y# o' ]1 ~
- */5 z! T! E7 ?- ], @ C+ n3 m, U
- public List materialsAll() {# v3 I# j- Z3 R" z9 v* V& U+ J
- List list=new ArrayList();
8 _7 L, v; V& W7 u' o - Connection con=null;
; N p O" q' }: }4 L - PreparedStatement past=null;' K6 U/ a: H( [: B
- ResultSet rs=null;
. R, K- u7 r% d, c( T& g8 M - //sql 语句" e+ M8 k" T$ S7 a
- String sql="select * from materials";
" ^: q0 Q3 d- D - //连接数据库
5 O- W; }6 v; ~7 U7 e V* P2 `. Z - con=MaterialsBase.GetConnection();+ b: p& i( N1 u* L
- Materials materials=new Materials();
# G6 k+ z: H& C! m3 t+ w - try {
6 H' @5 n/ I, ]8 Y B - //传入sql
9 V* T; k0 E+ W2 i8 K& j - past=con.prepareStatement(sql);
6 t* ^8 q8 J; y, t0 M' ~: r - //执行方法 K; v5 b% ^" @. ~
- rs=past.executeQuery();+ O5 W4 w$ t, O* N- H( @3 `
- //循环读取数据
) m/ p! H4 O: |* ] - while(rs.next()){
2 f# [7 @" s. S3 H$ Y - // id, name, spec, number, price, producer7 v, Y; q: [2 F
- //编号3 Q% |2 T1 ^# J6 z5 W
- // int id=rs.getInt("id");
; w* d3 u4 k0 M - // //名称5 D% ~1 g4 G2 n, m; W4 g
- // String name=rs.getString("name");6 M0 t: r2 F9 k3 u
- // //规格
- A2 L4 a6 ^ J2 i' O& T/ G6 \7 L' j8 j2 Z - // String spec=rs.getString("spec");
/ w/ r$ T" v. Y4 _# H - // //数量5 h0 ?4 w& p' }
- // int number=rs.getInt("number");
- k" P) j l& g - // //单价1 O9 J, {* a6 F
- // int price=rs.getInt("price");
1 M B) {+ w A# k3 i$ X( W% P - // //生产厂商4 r* N Y2 K7 O' X; u$ L& T$ o3 L
- // String producer=rs.getString("producer");* }* o9 v) [% }2 O- U/ |/ R
- // //添加到构造函数
' F) a8 u7 Y g4 c6 I - // Materials materials=new Materials(id, name, spec, number, price, producer);( h6 v5 D- p6 b4 h8 s1 i( y" v
- // //添加到list 集合5 y& ? d6 q$ G J* ?" }
- materials.setId(rs.getInt("id"));
; M, h1 J6 @9 h2 Q - materials.setName(rs.getString("name"));' l$ {/ n5 C" ?$ j3 |
- materials.setPrice(rs.getInt("price"));
: k8 N: d) [9 v; t2 F, D5 t - materials.setNumber(rs.getInt("number"));
E/ B% n2 J2 j - materials.setSpec(rs.getString("spec"));
" T( | A# V+ u - list.add(materials);
+ n. x K$ `( o - } 6 h2 i, C) s8 [3 T5 K9 g
- } catch (SQLException e) {( b# U4 q3 f" a; l3 W
- System.out.println("materialsAll 方法出现异常");5 b4 |5 r# W9 H, H+ W0 R" \, V9 N( w- N) `
- e.printStackTrace();
/ \) K" a0 V7 Y; {) L: \ - }7 B/ t4 E3 p5 o, G1 h: D
- 9 B- A/ Q7 d3 _- @5 B& M% @& y
- return list;
5 ~7 x$ p% \- a: k' Q3 w - }5 W" {6 \1 b# A3 \
-
$ [" h- i7 y/ x; Q; c) t- I - /**
' {- g* i7 L/ C4 A! o - * 根据id 查找物资信息
1 r$ T! q9 D% o; D3 {; i - */- r8 D: l e2 Y8 e& S
- public Materials materialsID(int id) {5 E6 @ z @( d* j, o2 W
- //声明物资类
p+ J' @$ ]+ W& }1 c5 a6 ^ - Materials materials=null;/ n S7 J' e4 s+ `7 L" O) ?
- Connection con=null;5 n( n/ `% k8 |, F' n( W
- PreparedStatement past=null;
$ }8 f+ E9 P: s; @- E3 Q6 a1 P& Q- m - ResultSet rs=null;
7 h* w1 L4 @* p' d, `* l7 F: l - //sql 语句7 u% g4 H' k& j3 T$ y" b" W7 R; B
- String sql="select * from materials where id="+id;' N, y' x0 W9 |. z
- //数据库 连接
/ y. ]) G8 S5 Q# q0 U4 v$ o - con=MaterialsBase.GetConnection();9 _( c7 y0 I8 U# c x4 Q
- try {
( A3 \" K. r0 K; [" h& ` - //传入sql+ B! A: K. E+ K
- past=con.prepareStatement(sql);+ G0 O6 H n8 e% k! U, w
- //执行方法/
! r/ K7 B5 W5 X - rs=past.executeQuery();
9 e! C. ~" w( f. ]# N" \2 @5 o - materials=new Materials();) |+ ~$ [8 |' e% I
- //读取数据+ W) Y4 b" C. j2 t; N4 i
- if (rs.next()) {9 S; ]+ I/ T, n2 s
- //名称$ S" s9 h5 K$ m" }' ]3 ]
- materials.setName(rs.getString("name"));* E' L7 a! j% A3 h F9 _
- //规格! s4 s" b. R! I0 i" C& z) m% X$ A2 X
- materials.setSpec(rs.getString("spec"));
* Q3 t. r. J1 y6 M$ D3 s3 Y - //数量4 W( ] p# Z3 K4 [8 E. g* b4 J% T
- materials.setNumber(rs.getInt("number"));
4 A$ o1 i3 `8 [8 T8 e% s1 u - //单价
# ^! R, w0 d! I2 {4 L! j - materials.setPrice(rs.getInt("price"));
X e( X7 e( d" o2 o3 G - //生产厂商6 O7 r3 {9 X: H1 h9 }2 k
- materials.setProducer(rs.getString("producer"));
# Y7 n" \' Q2 V; d& d - }/ V4 ?' \# M) c+ {
- } catch (SQLException e) {
% r' u: @: Y5 [$ ?. Q0 x: x - System.out.println("materialsID 方法异常");7 W3 k. E% M' ?( M
- e.printStackTrace();
1 W: g6 W. z9 Z0 G& ]4 c9 \ - }finally( b) ]5 X% Y. R) `& z
- {
0 y% q2 M4 p6 U9 o - MaterialsBase.CloseAll(con, past, rs);+ y" r& m4 c+ m: c. f5 p0 \4 ` D
- }1 \' F; R4 k% }8 r0 {8 K! G4 N! j8 S, z" t
- return materials;3 h8 t" b- F2 d6 U+ g' g
- }) C9 C! D& [0 e: i, I/ P
- /**( T7 u% y5 l$ Y) ` o
- * 更新物资信息. Y8 D2 C- _' D
- */- P; T/ u3 D1 V) |, v
- public int materialsUpdate(Materials materid) {( ^- ]; o4 l& @
- //sql 语句
: U* |, `" j; {% t - String sql="update materials set number=?,price=? where id=?";! J4 P" U: E, {5 F, O+ R" P# B
- //) I8 X$ a: V/ `0 P) |
- String[]getValues={materid.getNumber()+"",materid.getPrice()+"",materid.getId()+""}; g" U; [* r5 `& N
- return MaterialsBase.ExecutUpdate(sql, getValues);) Y2 Y d3 o6 S u7 W* B; G' Q
- }8 _5 j8 W% v G% {- b% K, x
3 y) s' M) Q3 h5 ^* U- }3 N9 y5 K- @) j0 k, E
复制代码
/ N1 r4 p* j8 @6 [7 B" W代码案例下载:点击下载
6 A( T. _: w7 ]* L8 h+ g
& Q6 l6 P8 v1 o |
|