该用户从未签到
|
板凳
发表于 2014-04-07 17:50:10
|只看该作者
- package s2.jsp.zhangxiao.daoImpl;
: T' f" p' {! J$ ~ t5 m( T; m - m, J7 ^. N% j: A
- import java.sql.Connection;
6 \ e$ m& I+ T6 P6 b9 Q2 f8 J+ u - import java.sql.PreparedStatement;5 ?1 M. b4 x; y: N5 E6 A
- import java.sql.ResultSet;
7 L! L- y6 f( ^, ^2 l( ^. @ - import java.sql.SQLException;
4 D9 J9 ~) k2 Y5 }: t, {3 { - import java.util.ArrayList;! M j( G8 [/ {# m: R4 o
- import java.util.List;' `% V1 U% @7 J% S
6 B/ Q4 D! `* I. i7 i- import s2.jsp.zhangxiao.dao.GameBase;
# G* q9 b# p/ d$ d) @ - import s2.jsp.zhangxiao.dao.GameDao;
0 `: |. |+ c6 b: F) y4 O0 {; e - import s2.jsp.zhangxiao.entity.Game;
( F3 T7 N8 a& ?
( D; ~) s3 o& P! C, q0 s- 3 X9 _% e7 D A5 r
- public class GameImpl implements GameDao {/ p' ^: K/ W+ M0 ^, |6 d2 l
-
: Y2 W- ]/ n& c6 t8 B - Connection con=null; //数据库连接/ ^- ]' j: G, ?6 K1 c
- PreparedStatement past=null; //执行sql语句
7 @; x0 y G' z% ?+ y h6 H- } - ResultSet rs=null; //结果集
& _/ z* {, H3 v/ X* N+ y) j6 w - List list=new ArrayList(); //list 添加信息
H8 z4 q- a9 d. h; ~. x! ~8 p9 d/ y - Game game=null; //声明Game类
$ `: X( d& |9 Z/ y, B' K - 5 r# `# u: S9 `
- /**
' m& f& r1 T9 ^1 I9 I - * 实现增加方法
; O4 X- Y1 o) K, E) p - * @author Administrator
* m6 Z2 p6 a" ~0 K2 L9 y" ? - *' b6 B6 N3 p3 j3 Q/ l- l% i
- */3 @1 S( S7 y, ^' V
- public int GameInsert(Game game) {+ d' w6 ^6 |4 L$ h
- //sql 语句
/ ]8 b/ ~9 J- W5 t' `, P - String sql="insert into game values(?,?,?,?)";3 i8 @& H1 j9 b5 |
- //添加信息
+ G a( g2 b/ D7 W1 H' p; N - String [] getValues={game.getGame_name(),game.getGame_type(),game.getGame_company(),game.getGame_year()+""};+ Y8 U- |5 { ?4 C+ {
- //调用数据库连接方法2 o- q% s% z, W9 c e
- return GameBase.executeUpdate(sql, getValues);
7 w X" Q3 I) W9 ^1 I2 L" H) R - } i0 G6 f" j* n: | [
- /**3 ]) P3 i% n3 Q, p- ~7 M' {; u
- * 查询所有游戏信息
( |/ L' R8 X. e. A! k - */) A% L8 s* K2 W& C! o: w2 [. k
- public List GameCount() {% e+ ^9 r" |7 K" \$ x M
- String sql="select * from game";* ]; {3 P. e+ M; X S# {% Q- M
- //连接数据库、- ^9 C( j9 g6 L9 Q# ?
- con=GameBase.GetConnection();
# Y$ v; n, ]5 F; G - //传入sql 语句
1 t+ ?' q* a( K# J. t+ b+ Z8 v - try {
6 H" p. v" b) K# D n# @( y - past=con.prepareStatement(sql);
9 i3 a6 ~; I% ]' `5 c - rs=past.executeQuery();. C% O' i" j- f# E+ r5 v
- //读取信息
( D3 C% O% v' D% ]& _1 e. m - while(rs.next())) R% [( p' A0 Y# q! F2 O
- {
! A0 \/ b9 Z, e0 B2 ~. M - int id=rs.getInt("game_id");/ r: l; J, E6 D% t: |8 M
- //游戏名称4 W; f S# U( P; o$ V
- String name=rs.getString("game_name");! b7 \0 F7 W5 t' ~
- //游戏类型
& Q4 q" r; q" \4 R9 `/ g - String type=rs.getString("game_type");
/ b$ r. g2 o! k: g7 V - //游戏发行公司
' b# b# b$ D' o1 O - String company=rs.getString("game_company");
3 A* {/ X; D5 b) A - //游戏发行时间0 D) N* L X, t5 N# }
- int year=rs.getInt("game_year");4 g, U4 M- [$ |6 S6 c
- //添加到构造函数里
* v/ I! `' I$ K$ R- I - Game game=new Game(id,name,type,company,year);
" h, `1 z# K) i - //添加到list集合% }% X1 c! O1 G+ p ] ^; m: d
- list.add(game);! z* x' m) z7 I. b9 y; k9 g2 C0 t( U
- }! r# u8 P, b+ _% O7 @- |
- } catch (SQLException e) {4 ^+ K! T; u$ {
- System.out.println("GameCount 方法出现异常");
8 m3 R) E9 l- M - e.printStackTrace();
% A; k$ f% c' l5 I - }finally
7 h( H, Y. ]4 `5 h1 W* b - {4 D; }$ ?4 C6 H4 i2 H6 O
- GameBase.colseAll(con, past, rs);
/ ?* t+ _# P$ V- K# g - }$ z7 ?: k0 n$ G5 z ?; W% P- d. X! _
- return list;" H7 f& [% V" b; T1 I3 i- B* ?
- }
) n, I9 }/ M3 x7 t
, D" A8 c$ j1 l- /**
1 j0 K, H3 S9 z - * 多条件查询
- ]2 Z7 x5 n9 k3 ^/ Q5 Q5 c - */9 G: z! l/ a$ |1 \6 {5 _% g
- public List SelectGame(Game game) {# H1 Q. m/ S6 y
- List list=new ArrayList(); //list 添加信息
~ z8 H+ I. \, H! k - //sql 语句
& C/ O3 t) G5 x( H4 a - String sql="select * from game where 1=1";
; Y5 }: l0 K' d7 U6 r - //连接数据库
' n3 d, ?% \# `1 y' H; s( P/ f - con=GameBase.GetConnection();
! f! M8 s- r2 E% _% m2 t - try {
0 e! A: H5 e, {9 {7 s - / u; h/ x0 Z$ \ x
- //根据名称查询时
* {/ T! I0 |0 n" _ - if(game.getGame_name()!=""){; o, F6 ?4 r( o. B' }: `" H! H5 L
- sql=sql+"and game_name='"+game.getGame_name()+"'";6 j: K" I1 \$ ]; r
-
7 H0 r( t |$ r1 o: _0 \ - }
5 w7 H3 l' \3 R1 m6 ?7 N - //根据类型查询时
, H1 s, H- u: S, A - if (game.getGame_type()!="") {* |0 W" Z( }8 p" ?1 K7 ^& }: L5 x
- sql=sql+"and game_type='"+game.getGame_type()+"'";
3 u! }% P' S" y - }; p m& E+ ?' Y) ]( H
- //根据公司名称查询时6 m' n3 w. O! d. _ i
- if (game.getGame_company()!="") {
1 o: b E* r4 H" K - sql=sql+"and game_company='"+game.getGame_company()+"'";
1 W& h" I' r; E+ z - }5 ?9 Y7 B/ y G/ s( o" H! h5 \ H9 S
- //根据年份查询时
; H9 [# [+ B- Y# M - if (game.getGame_year()!=0) {6 C2 ]; V7 \& d& K( d" t
- sql=sql+"and game_year="+game.getGame_year();
I. Z& {( m4 o( t - }" G5 }/ Q* o, y
- //传入sql! N) K* M' D# ]2 r
- past=con.prepareStatement(sql);
2 Z, ^/ ]. J/ N! y0 y - //执行sql
% I, }: ?" @0 g) p9 V* i - rs=past.executeQuery();% j; x! V" x, t4 B7 W, E
- //读取信息" i+ d* Y7 O. ?0 W/ J; _# n
- while(rs.next()){: k- m% C' L' m
- int id=rs.getInt("game_id");
1 M* U' Q5 G F- B& p6 \ - //游戏名称9 |9 A' b, q. _
- String name=rs.getString("game_name");
, x6 _3 A0 }" s! b1 c; b - //游戏类型
; p+ q1 e# U& ?% w9 j. { - String type=rs.getString("game_type");) n( }2 i+ M% v' B4 Z% L; R
- //游戏发行公司
/ ` J2 A' _- p - String company=rs.getString("game_company");
4 {: U0 H: }! P$ Y5 e4 S - //游戏发行时间$ O: f& X- g. q+ H
- int year=rs.getInt("game_year");- y5 |6 c. e% S2 c/ V
- //添加到构造函数里5 a7 B# w1 j" ~2 u! I
- Game games=new Game(id,name,type,company,year);' w1 d N; _5 ~- l
- //添加到list集合7 q4 F6 y1 Q N1 R* x0 Q& f
- list.add(games);# a8 Q! x6 G% c. T$ ]4 M, U
-
' p9 j0 x3 _7 _' q4 U- |6 \7 R" n. Z - }' G- J( Q' r6 D! {! [$ Q3 F
- } catch (SQLException e) {, B) c! U! ^# f3 z
- System.out.println("SelectGame 方法出现异常");
' E+ d2 r* N) p, J- Z3 K - e.printStackTrace();0 A5 P% \ y7 Q b6 o5 O7 H
- }finally{
. T Q( R3 J. d% [% {" h4 j; i - GameBase.colseAll(con, past, rs);0 g4 E8 [4 }# U6 k
- } N$ d/ v9 K( G* c: h4 _2 b, ^
- return list;6 F- v1 [. h8 u+ f
- }
& I; u' d" O5 C- q/ b: N - }1 C* t0 ~8 i7 {/ A' {9 B
复制代码- package s2.jsp.zhangxiao.entity;
" q' O4 ]: F; G. M8 ]. \
. c X- |( h/ _% g6 o$ _5 I4 A- /**: c. }* p$ a4 q
- * 游戏类) V) x( _6 H9 p9 w: @
- * @author Administrator1 O' M, E( r7 s* l0 I {2 Y
- *( N( N: M1 Y* j3 l
- */
, o! n( Q7 V) x8 N) m' G; n - public class Game {2 a6 @: s/ ?0 E2 n, ?+ x. b
- + l( T" j! ?9 `9 N: s. U% o, J
- private int game_id;
' B' {( L% J) y4 k/ Z- P - private String game_name;/ X) m0 w# d$ d T* g
- private String game_type;1 {9 t1 _1 q7 q. w+ [+ h$ x
- private String game_company;
! d; Z/ q+ [$ m7 l/ @3 X* W - private int game_year;
3 T! j$ W! C/ e. U1 _# b+ o - /**
& N% d& X) S8 J4 r% e9 K - * 有参构造函数; q8 ^7 X9 n3 b
- * @param game_company0 Y3 I6 t! q1 K; D/ d+ \5 i4 }$ F, f- a
- * @param game_id" n, }, N$ V9 a8 Z& b/ J- g6 e
- * @param game_name. R1 u* v' S9 g! a2 g
- * @param game_type
. A: R0 |) l1 r0 b/ ^! V1 C" K - * @param game_year
. b3 W/ C3 T: t8 U ]* S - */, ^! K" S6 W0 Y' ^$ D
- public Game( int game_id, String game_name,
; l0 T: `1 ?! z5 S0 B - String game_type,String game_company, int game_year) {
2 } f1 [4 d+ [. [ - super();6 S7 O, }9 [. r. K m
- this.game_company = game_company;8 Q. G1 D7 R! z
- this.game_id = game_id;- }( f& j8 r% L1 |0 n+ R
- this.game_name = game_name;$ h$ ^* j' A; \4 {: m' Y
- this.game_type = game_type;8 `7 N' \6 v' S1 r/ O1 a% ?
- this.game_year = game_year;. ~, X7 _( e7 a' ~
- }
/ q, D: o9 u, Z4 F - /**- r6 ?( e( t& r# _ v$ c
- * 无惨构造函数: U) @8 w/ c6 p: C4 H1 V+ I% D
- */
, T9 L8 F& L, x1 e - public Game()
7 {, T7 X% `( E - {' G+ w2 V& E+ r: X: G
- 2 y) S4 D' U6 M, @+ m
- }
& P5 Q8 X5 @* } - /**
: w; T: [% Q; A8 P% j - * 游戏编号
1 I% j9 R; @) K% U+ J. L$ n - * @return3 r9 @3 \3 A B
- */
6 R! I4 z# O" y9 o/ r# d% A - public int getGame_id() {! _2 U6 J2 M! c: G
- return game_id;& a9 a% x d7 ^6 A
- }
/ n, h: z8 I0 X U - /**' ~+ J9 u7 l' o. o$ q- n5 f
- * 游戏编号6 g* u8 [% [- H* ?
- * @return
: A% v0 v: Z. q7 ` s7 ]( o/ f: f - */5 @, T% t0 V2 [- Y! `% v( }( S% ]" \
- public void setGame_id(int game_id) {( ~% W" k7 p& T/ a
- this.game_id = game_id;/ X! X# X1 y8 K# V+ t9 k$ o
- }
" W/ A& x+ `. A0 j% p/ h: s - /**: M* X, u' L. L8 z0 _
- * 游戏名称+ y' n6 n. [0 Q4 Q6 d( y! m! J4 K# z
- * @return' I* w$ |1 P6 S* C6 s/ d4 Q% E
- */
& n6 W7 a5 C- m# N1 C - public String getGame_name() {' \3 Y, |5 Y. F3 ~
- return game_name;9 B5 ]: ~$ z1 T/ h
- }/ o. A. z2 R* G( Q8 C9 a, @: Y
- /**5 i/ [; b9 W8 H V' y3 L4 `. C! k
- * 游戏名称 D4 j% l2 y! p3 W2 ^! }
- * @return
4 K5 j- r* l/ F$ y1 ] T, E2 E: c9 b - */
1 V A" c, X9 [! K' ^ - public void setGame_name(String game_name) { i c! c% O; F8 q
- this.game_name = game_name;0 t, a9 b6 H/ T p, g2 ?$ e
- }
4 |; v$ Y0 G8 C. |- Q( ]" X - /**4 x h F7 h5 i) |$ v5 ?/ ]- v
- * 游戏类型
C# n# D! f" D8 N4 b$ ? - * @return
2 p+ Q( @* d( T - */1 C* B5 F4 f7 @ v
- public String getGame_type() {
, c1 M: K. V: f- k* O5 ?5 L/ @2 O+ l6 X - return game_type;( h' k6 U! c% S( W
- }
+ H. k/ A0 R' h+ z - /**8 {0 d4 J1 r4 @( F' ~
- * 游戏类型) N$ \% T7 F+ |7 D5 ^; q
- * @return
9 W2 ]3 D" a& d# p( ? - */
$ \, e" _ X8 E) l9 ]2 D* @ - public void setGame_type(String game_type) {
+ a" m6 ]. h, ]- X+ e0 G: {9 K - this.game_type = game_type;0 W5 x: o8 Q! \5 c0 ?
- }
' ]( @0 `& R9 n3 q0 ^: D/ K - /**
; e3 l% N9 e5 Q - * 发行公司/ }9 K9 ^& b: r) X5 {6 c5 I
- * @return
: N- H0 k( a) `+ w3 x - */4 q1 u" H: \) o9 z) Y
- public String getGame_company() {) j2 Y( w( a# m3 d* {5 W
- return game_company;
& N+ v0 M, v( w$ H3 A6 l- @% u) x - }4 z( F% C! l! f4 e6 w+ G4 Q6 P3 F
- /**
5 ~/ ^5 b- {) m) [ - * 发行公司
6 r) E* A/ t3 e3 v: L - * @return
+ q3 P$ z8 [! _, a: d- [. l" G - */- w! d5 A1 ^, I4 B; h/ X h' d
- public void setGame_company(String game_company) {
6 O* k/ w8 x4 N5 p; w$ | - this.game_company = game_company;
0 B$ B/ U/ ]4 { h+ J2 ] - }
8 L9 {4 y J/ e# ~" ^+ z: C - /**
* Z: x( A: a( M( | - * 发行年份
$ h: J) Q7 o5 i6 L) R( B* n - * @return, X6 W% l P5 T: x6 B3 h# m* B
- */0 l4 G7 E ]6 @+ V6 J* {, C
- public int getGame_year() {3 r( Q# w: \( M/ W; O" V
- return game_year;: Z" \1 [0 z9 R+ e2 V& A! y
- }
8 b) [1 @: Z. A1 `! p( e - /**. s% C9 `" d- p% v j# U
- * 发行年份6 b$ H% c* \' \( f8 Q
- * @return
4 x' [7 ^9 f6 i - */0 `, D* E$ r' y: z+ ~+ o8 C' x5 Y; c
- public void setGame_year(int game_year) {
8 t: |4 D( F" y* K) O. ?. s$ t9 l - this.game_year = game_year;! ^ h! _) v4 |2 d* S( t
- }
: r) m# t7 m) c; T - }" s) u( ^; M8 g. m% l
复制代码 5 I& R$ h9 y% V% h1 u
6 s; U% T- r/ k' S
8 \1 b! R2 q W, I u |
|