该用户从未签到
|
板凳
发表于 2014-04-07 17:50:10
|只看该作者
- package s2.jsp.zhangxiao.daoImpl;# H/ s7 _ _9 a6 s1 U
- 9 v! p% j+ m1 T. A! _; M
- import java.sql.Connection;
9 \! U! \ m, _# }; C - import java.sql.PreparedStatement;
1 w u' x/ q( E - import java.sql.ResultSet;6 P2 }% m) J& p" }7 Z
- import java.sql.SQLException;
/ _1 p# c7 B& T" ~4 s( c. D - import java.util.ArrayList;# ^! C; q/ _! J, M6 W
- import java.util.List;
, F& j( Z, i3 B: Q( O) u" N1 u. R
" g x* k2 x; x" x( K- import s2.jsp.zhangxiao.dao.GameBase;
4 a/ z( X2 d# S4 x - import s2.jsp.zhangxiao.dao.GameDao;- @. P( n0 M# ~$ I
- import s2.jsp.zhangxiao.entity.Game;
7 ~9 q+ m; i# ]) N4 D - * ]9 G2 _7 m. I. d( W
- 2 |3 X7 [/ x0 _; \- J
- public class GameImpl implements GameDao {) z* v" _. I7 }5 q6 C
- 3 s2 J7 G! [0 [6 A4 J
- Connection con=null; //数据库连接
- n+ G& [1 o) f% j, `/ n+ Q6 v - PreparedStatement past=null; //执行sql语句) s+ s2 ^' ]. H2 O1 B, P/ e
- ResultSet rs=null; //结果集' e/ `* S5 r& ^/ H) H4 h, i' {
- List list=new ArrayList(); //list 添加信息/ P* x0 E; z4 j0 E; ^7 q4 o$ w" p
- Game game=null; //声明Game类* u5 g6 d( e& o0 l: J6 P" V
-
0 z U! a% I6 F) m" u2 U1 Y, l - /**! L- {# `+ W; Z7 L6 e- D
- * 实现增加方法' D2 s0 v# ^, s8 C
- * @author Administrator
9 R% }+ d. r6 w2 o T - *) e# w1 S- f: Z4 j F+ [2 j' b5 N: m
- */
% j2 u3 t4 p* _* \$ l- ^, G; L - public int GameInsert(Game game) {- o: x! i( r R5 N& }# {' d
- //sql 语句4 K1 o: Y& |, e1 z+ x
- String sql="insert into game values(?,?,?,?)";- w! M% G( V" n' S ]" E
- //添加信息
( T# P# T$ m3 i, g/ Y0 _ - String [] getValues={game.getGame_name(),game.getGame_type(),game.getGame_company(),game.getGame_year()+""};
% L' V) `! K! a8 |- M - //调用数据库连接方法7 V& C' U* s5 O, ?8 M
- return GameBase.executeUpdate(sql, getValues);
5 D/ N; c7 c) D+ [/ v7 _2 S - }( ]9 l# g8 q& F$ x: k. ?1 m5 ]% P
- /**) x/ L, b5 h. c0 E
- * 查询所有游戏信息/ F# E. C* E y' J2 a" i9 I
- */
0 i) |9 C y, n! D6 ?0 T1 E - public List GameCount() {
9 G4 t$ v5 d. x/ {' b - String sql="select * from game";6 J6 d0 Y1 q! b! t! s& D
- //连接数据库、
6 `2 j' e2 H9 ~/ z - con=GameBase.GetConnection();4 z# d, j- q. @% k$ k
- //传入sql 语句, S* s* @/ h' x9 Z2 \; ?) n
- try {* |; }: L8 O1 s
- past=con.prepareStatement(sql);$ t$ L0 b5 H/ `, X
- rs=past.executeQuery();, y" Y3 U/ O+ I7 @; c, L
- //读取信息
l: H3 L3 g* t0 b - while(rs.next())
# [8 t J1 ^4 { - {* W/ r% c$ G8 y" Q+ @) t) \# A, u
- int id=rs.getInt("game_id");
$ M! P$ p" Y7 a3 L - //游戏名称6 } a. o6 A2 h1 M4 t# V7 C
- String name=rs.getString("game_name");* i# A/ }# g* F, z! ^
- //游戏类型
' L0 \( {9 D. p1 X - String type=rs.getString("game_type");
( s, z8 a+ V, q- Y - //游戏发行公司
9 H1 q; P! P' S- ~% ?. F$ t - String company=rs.getString("game_company");
3 m! K" o: P- K$ t- J3 S - //游戏发行时间 y: S2 |( f% [/ f8 `3 @
- int year=rs.getInt("game_year");6 ^1 b8 V2 B) n, Q0 f9 F( K6 g
- //添加到构造函数里
2 J3 H1 e: J* U' l; N7 h; z - Game game=new Game(id,name,type,company,year);
: _% Y$ g: ?4 C# k/ C ] - //添加到list集合. S/ n+ R7 Z2 u5 \
- list.add(game);
/ }1 A3 C: R/ ?: C/ j, v; P - }3 w9 c6 t6 s$ d; o2 R& z! D
- } catch (SQLException e) {
3 H2 I* _3 r7 |; g- ~$ G. z H - System.out.println("GameCount 方法出现异常");
: E2 d4 d) T. p - e.printStackTrace();
8 q z5 G* q1 d8 r5 T) }6 S. Q8 e, P - }finally6 l! E1 l" ~6 E# Y0 e, W
- {6 Q0 [ ~4 M& V" f; u; D; | M
- GameBase.colseAll(con, past, rs);( S# @2 j7 [+ D1 B- J; g/ e5 Q
- }7 J& d. g, Q5 B
- return list; [6 K3 ~( Y0 R! L R
- }
2 o% i0 J/ ]% Y5 Q# g
* S, u6 Y7 `; Z: w7 y- /**4 l4 c1 J) G+ k+ N" A6 G, g# @ `% C, m
- * 多条件查询3 v0 x w1 k( F/ m: m, p
- */( A+ i( I! o g/ ]1 f9 Y% Y5 i2 Z
- public List SelectGame(Game game) {2 A; \- H8 _$ d2 \/ ?# M
- List list=new ArrayList(); //list 添加信息
% H' D3 k' h6 @/ w: d5 E - //sql 语句
+ _3 L3 `& W' c2 |1 M9 c - String sql="select * from game where 1=1";! ], i2 h5 j$ n
- //连接数据库 {6 Q, ~/ D: ~" S, r
- con=GameBase.GetConnection();& |- ]9 V0 D. d/ Z' J/ l2 r0 f
- try {
7 o7 J9 I9 K$ ?6 L4 p# E - ; ^; Y6 A; H9 \5 `+ v
- //根据名称查询时7 b; v3 N' S5 Z l% v2 U
- if(game.getGame_name()!=""){ y( C( g; a/ M
- sql=sql+"and game_name='"+game.getGame_name()+"'";
! f( U1 H1 D, g - * x& O. L A5 F) d7 v% \) l" S
- }& s- ^( \# [3 k0 ?* U4 a# l- m- y
- //根据类型查询时 x# D$ ?, d! Y+ W; _3 ?9 L
- if (game.getGame_type()!="") {
8 A @# _2 m6 n5 p# a0 @ - sql=sql+"and game_type='"+game.getGame_type()+"'";- i+ m. }+ j' N& O
- }
5 R' O7 n6 I; l& Y9 e4 R6 Z7 K* l - //根据公司名称查询时 T$ Q& A7 m* G' p# ?1 I- b
- if (game.getGame_company()!="") {
" T) Q% Q% a& t+ m! u) a2 W1 m - sql=sql+"and game_company='"+game.getGame_company()+"'";8 z% U! K; h2 r% k) Y
- }1 s: @/ ~- K @" }# Q
- //根据年份查询时
$ D4 r! S b+ i5 c( p( A; Q4 Y, A - if (game.getGame_year()!=0) {
! I2 b) j/ l- `. f/ q$ @ - sql=sql+"and game_year="+game.getGame_year();
, }7 B# \3 a8 i9 r4 I3 Y - }# u% r: X, N3 n
- //传入sql
& w5 L K( t7 U- M8 d - past=con.prepareStatement(sql);" c% y8 ^1 j4 R# O/ e9 D, D
- //执行sql
( t) k6 A L4 {- e! Q& z - rs=past.executeQuery();7 V+ @! }" z7 E
- //读取信息4 c( A0 K; M5 A7 x7 V @
- while(rs.next()){# M8 l0 D/ I5 _* L/ E8 y- H& w
- int id=rs.getInt("game_id");6 H6 T! M5 Z$ Z; ~3 g* C
- //游戏名称& I' c0 G/ A- j0 }& C
- String name=rs.getString("game_name");
2 E6 u) G/ K6 Z+ _. o: o1 Z( U - //游戏类型
- }# C7 I$ k8 R! J! `. H - String type=rs.getString("game_type");
2 o- F7 Z. U# C- {2 @9 b - //游戏发行公司( J5 E( F! S& O# a+ q
- String company=rs.getString("game_company");
9 [3 K& W5 `2 h7 G/ c* ~0 U - //游戏发行时间* n3 Y$ D. ~$ r* `# y
- int year=rs.getInt("game_year");/ o& s" d6 ]! E
- //添加到构造函数里" _& ?2 k5 d$ g0 v
- Game games=new Game(id,name,type,company,year);- t# h8 m. w: k& S' l6 g, G
- //添加到list集合/ C8 g: H' u6 P; v# t5 C- Y
- list.add(games);
+ _7 B/ H: C/ O/ {' t! j - 6 o6 r- o# z2 W1 N
- }
! q; Y: O- w* t; ^/ M) O - } catch (SQLException e) {
' S7 D. X$ w+ ^ - System.out.println("SelectGame 方法出现异常");* ~- i& }1 q4 o3 k
- e.printStackTrace();
& ]. X' q2 P0 |7 S [ - }finally{
) [5 d3 t0 s9 S# w! q' `" R6 G - GameBase.colseAll(con, past, rs);" d! a6 b5 y) Y, N' l9 [
- }% G1 @7 i2 M5 E: f5 N k
- return list;4 X$ x2 ]1 l$ ~3 h# q3 L: H G6 ?
- }
$ F; Z, d* x/ \9 B& ` - }
' d: F! z# e. d; S$ V& V4 E* b0 g
复制代码- package s2.jsp.zhangxiao.entity;" R$ [6 b3 e, }* i& h
- 3 T4 e# i( `* n F3 s- z) _
- /**% e( s: y6 ?2 Q
- * 游戏类
* V2 f5 a8 R- G6 i! Q/ o5 F - * @author Administrator+ P9 D; w# [$ I- S4 M
- *; C7 A3 ]( A1 p" T3 W
- */
; e6 U' V8 C# \# }4 I. V - public class Game {
( c9 K' l+ G# y0 C* z! Z, y6 a - - C- M i8 P( ^0 e! p* q
- private int game_id;, j1 d& F9 C, A0 J2 y/ d, e5 ^# P
- private String game_name;
3 m! O2 i8 ^0 A4 S' n" G - private String game_type;
2 P: X1 Z7 o5 M d; l- i - private String game_company;3 k' B4 I4 V% i L5 R7 o" v
- private int game_year;
! P) ^: C; X' W' Y4 G - /**8 s& l) Y$ g/ E( n$ k$ y
- * 有参构造函数% C1 h* W. x$ k
- * @param game_company
) y& W: u& W [4 ?; Z$ }+ Z6 E9 a - * @param game_id8 e B' x; a% h. h& o9 d4 O$ }3 y
- * @param game_name8 u" k7 a7 i6 q% M3 s
- * @param game_type7 P' U! k% L2 ~" ~% T
- * @param game_year
# A# c( Z" c& t* t2 ~ b - */
" q) S/ @/ [9 s! h8 a' b - public Game( int game_id, String game_name," H" M& E8 ]0 m6 F) X1 w, Z
- String game_type,String game_company, int game_year) {
* z; d; f- A1 I- c. K& I. ? - super();
0 T' Z3 u/ x/ s4 g - this.game_company = game_company;
0 d$ s, e3 f3 g' \: T! J3 i - this.game_id = game_id;
0 a w" G! \% f* R% K7 V, M, K - this.game_name = game_name;7 y7 K9 @. d3 c$ R
- this.game_type = game_type;9 [" D8 B4 k- H1 |* e( E" G
- this.game_year = game_year;( @; O" ]& w9 N7 O9 ?! t/ Q: N, o
- }. ?7 O/ U9 m1 [$ C! }( \6 o f& u
- /**
' |% f" a4 `9 Y" S5 J1 A. ?6 @8 Q) X - * 无惨构造函数+ l Y" m, z* n1 u: ~
- */
[, I: S9 B$ G b! q8 K4 d4 E% @ - public Game()
- u3 _ @4 f) n) ?7 o - {( D- t3 k+ A3 M8 g& y" G4 g
- - r+ g4 r2 a* X0 [2 E' w! q
- }
( [ D4 C5 i$ v* U - /**
- {3 ]+ a B0 a+ m9 X5 V+ J - * 游戏编号" s, X# Q) L" |; c# l
- * @return
9 [9 n) l( {8 X: M* m; B. h: z - */
: a3 j, z1 U" l3 C& B6 x - public int getGame_id() {" K# J3 X7 @$ t' `: Q
- return game_id;. T8 p& r6 ^6 g! U4 p
- }: g5 ?' a. @' J& S! q3 ~
- /**1 m$ ~6 E5 g( u, |
- * 游戏编号! y# l& ~4 E6 o, Y" E$ S+ o
- * @return) h* x! q2 F$ m8 b
- */5 o- T8 F* F: h# [
- public void setGame_id(int game_id) {
* D% C4 a; L2 B! L - this.game_id = game_id;
2 }. B5 L7 x- J - }
# X% {8 ^! i9 G/ h: P- f - /**
& O' X K8 ?: s# k - * 游戏名称
/ y/ U0 w! E$ p1 V% p4 z" u - * @return
8 v, C: W6 Z, ~7 p4 m! N - */
; v8 X( j4 B5 v" `+ D, I - public String getGame_name() {$ ]( Q8 L- K6 Q5 K1 _
- return game_name;% F) v) g; _8 Y
- }' t0 n5 l, G+ d+ ` }# i# H
- /**
# O6 x0 Z* [0 G! a; K. J3 a - * 游戏名称
7 K% S/ T& E: A2 R6 x- Z$ n# \$ A - * @return
6 v( v0 x% Q( j- O7 N$ `+ N - */" M9 K+ I* x* h( O6 k0 x3 X
- public void setGame_name(String game_name) {0 f0 k2 K' L$ E$ `3 b9 _
- this.game_name = game_name;
! [# j! a9 o3 H9 e& n/ u; ` - }
' e2 z) e. E3 F* g4 o - /**9 _+ p9 A/ |3 `: E& s4 I) z
- * 游戏类型! |& d! e6 h( y$ U
- * @return
! p! H9 j# [% y5 C - */
; L( s. h- _% N6 p2 S$ @& Y - public String getGame_type() {$ j7 e9 N; S- d# B, J; ^/ v# x
- return game_type;
6 e2 K* F. R3 K$ u: p - }
- p3 g% J9 r, y+ \# ~: d - /**# G) v* u5 W6 P
- * 游戏类型$ A; E. q- j; z' k1 y1 e6 B
- * @return T* G4 ?" }$ y9 g! I$ C
- */
$ t% T: o* b" t# W' N& U! r) C - public void setGame_type(String game_type) {8 B/ S* H5 X' s4 A+ j7 u/ Q! b0 `
- this.game_type = game_type;9 m) U' Z i/ v7 Q
- }+ n+ l1 d; k3 y8 A
- /**
# n% N& c: c% ?( U! ~6 m5 m - * 发行公司
0 {9 a1 {. k7 P- D - * @return, r) L2 Q, M: m m4 E
- */6 V" b7 X O- t j6 E% Y% `
- public String getGame_company() {
+ \2 ]) X" Q% s' {5 u - return game_company;
3 U/ o( O# F, U, s7 K - }
6 T' h" w Q8 s9 T# _* [0 R* ?3 ? - /**4 h8 h4 u# b: j3 P7 Q/ p
- * 发行公司& r- {: Z$ x( ?! {/ F
- * @return# x& q& ~6 ~; L8 X- c) k
- */
: {2 o. l( |- E9 ]$ m+ | - public void setGame_company(String game_company) {
3 ^) _) K" F, E3 O3 E - this.game_company = game_company;
" X( @$ _+ M! h# `+ a2 O7 v+ C - }
8 @. r& @$ G/ }' {) y/ ~" ]5 E - /**
8 m) c8 F& c1 z/ _6 Z" L! x - * 发行年份: }$ A6 v1 w8 u2 o0 f
- * @return. X$ S! L( a8 I
- */
+ {- I: O5 C5 b+ P - public int getGame_year() {" l( h% q/ D" W0 R( G: {
- return game_year;1 p. H. s& H5 E$ |$ T* y
- }
( D8 u) m0 v& ~1 P0 g' g - /**5 ?! Y2 Q) G9 d; U5 P/ `
- * 发行年份
, E3 J" q l' n$ @ - * @return
& N$ ?8 @* h9 w, ?/ s - */
1 V6 g% s6 ^# B! z3 E - public void setGame_year(int game_year) {
" P0 e# p$ O$ S- C$ ^6 s0 ~ - this.game_year = game_year;' A; H: f9 Z# v1 J( s
- }
, b8 H4 e/ y6 d0 N - }
3 r4 q: j5 C" g5 ]7 s, U+ ]
复制代码
: i6 Z# [6 \, k0 k3 ]2 b
4 [5 D% N7 \ m/ _4 P }0 x7 I# s T
|
|