该用户从未签到
|
板凳
发表于 2014-04-07 17:50:10
|只看该作者
- package s2.jsp.zhangxiao.daoImpl;7 D; W& H: G$ V% _$ K* H2 ?
- ! L, ?6 y! h* A% T
- import java.sql.Connection;
1 K% T* K; w3 O8 D - import java.sql.PreparedStatement;9 z5 | R$ ~ s a
- import java.sql.ResultSet;8 T) s; l1 y6 S- O" z0 n0 f
- import java.sql.SQLException;
+ v* A. Z; F) g! {/ g - import java.util.ArrayList;! C" u" p, J, g' A4 K
- import java.util.List;* R8 c4 Q0 |* v0 l% o1 O% f
; }* k8 j. o% Q- import s2.jsp.zhangxiao.dao.GameBase;8 i6 a. U( Q) e9 _; u( M' S
- import s2.jsp.zhangxiao.dao.GameDao;4 V6 ~* p* V; Y* T( w" k
- import s2.jsp.zhangxiao.entity.Game;9 a" p; M% m i* s' T
- * m! L& c' G# ?/ k4 s# g
- ) v- M9 l: u2 C3 L- ?
- public class GameImpl implements GameDao {( l1 l1 }4 f% j3 n/ g3 x) R( Y
-
4 f9 v+ d: l+ ~2 {+ \$ w4 m8 g - Connection con=null; //数据库连接
% U$ v2 d2 s+ z - PreparedStatement past=null; //执行sql语句0 c- `, C8 V, ~$ m4 Z- N6 b2 }
- ResultSet rs=null; //结果集) o% B5 F6 n T8 R h) M1 J
- List list=new ArrayList(); //list 添加信息2 i" Z% b- ^2 k
- Game game=null; //声明Game类
' Z9 A d. D P, f - ( M! K& _7 K W6 V% q; E$ ]
- /**. V6 }& N5 y- g, \3 d
- * 实现增加方法 R3 |1 ]1 K& U( a$ }" K' N
- * @author Administrator# Y; n; n/ h: W2 B9 c
- *8 t" I2 r- A3 Z# \/ X! a
- */& Y/ M0 R: [5 E- c* J- l( c
- public int GameInsert(Game game) {; x7 J8 [( L, A& t4 Q
- //sql 语句" V5 I v: r0 S. o. }
- String sql="insert into game values(?,?,?,?)";; z6 r* }2 r$ m+ d. y3 `
- //添加信息
, Y% f( L) R5 \ - String [] getValues={game.getGame_name(),game.getGame_type(),game.getGame_company(),game.getGame_year()+""};2 t6 M0 g% w' L" r0 x. C% z# h( G
- //调用数据库连接方法
' I# v( x3 B7 E- _: X9 L - return GameBase.executeUpdate(sql, getValues);# x6 z8 ^: q4 }9 i! ^& a. X! v6 H
- }' l( J: k( u' _# S
- /**
2 ?5 e* s* P7 J - * 查询所有游戏信息/ o: J5 k& E- D( l0 C
- */) r0 @8 c7 n3 I' r( A
- public List GameCount() {+ n9 e/ Z2 |: M1 R
- String sql="select * from game";
/ R2 O; s! B, a4 v6 e6 c8 l - //连接数据库、% ~) w7 l' t0 S/ j u" ^5 N* K. U
- con=GameBase.GetConnection();. O) c, r5 [3 ?# Y. f
- //传入sql 语句; p/ K. h( a+ A- }/ K* d
- try {
! q% \; l( Q$ @# r - past=con.prepareStatement(sql);
. ~$ [" \& b6 E. p% _ - rs=past.executeQuery();6 _+ e4 Q+ G4 L) I0 w7 t0 v4 f
- //读取信息3 ~; u+ h$ h( a$ n+ n2 D1 F+ d
- while(rs.next())
( I! {& ?* V r5 Q- D% \. { - {+ ^5 S9 w/ f$ X1 s6 x
- int id=rs.getInt("game_id");
; n; F0 C" P. B9 R8 O" } - //游戏名称
G; C( O1 e U& _ - String name=rs.getString("game_name");
7 v; p0 d$ k' b7 F9 ~1 c - //游戏类型
& t& r3 a. M g% t- j+ y/ @! ^ @ - String type=rs.getString("game_type");5 k4 R' M+ ~0 y$ S3 Q6 l
- //游戏发行公司1 Q- g' R) l: x% X
- String company=rs.getString("game_company");0 s( g+ }3 {3 C* ~( U i2 ^0 H: ]
- //游戏发行时间
+ b4 j# H H& W" J, r" ]3 L! G" r - int year=rs.getInt("game_year");0 @) O8 S: w; H
- //添加到构造函数里' i A4 y* l& g6 a1 x7 T8 G5 o' x7 E/ O
- Game game=new Game(id,name,type,company,year);
! e# p! f t1 u7 q - //添加到list集合
2 }8 s% x! I6 [; r' }" K; l - list.add(game);
& r. N. P, O+ |9 ^: G. l5 F2 P - }$ h7 N. {+ R% J, R
- } catch (SQLException e) {
; H/ D6 b3 ?* @2 F - System.out.println("GameCount 方法出现异常");9 R+ P% N$ ]$ i
- e.printStackTrace();
3 U% |! K& H% Y3 c2 s6 i9 G. e - }finally
* C8 K9 W* `3 @ e" O4 E0 _ - {
7 O% z, F1 e' D+ M, O" y - GameBase.colseAll(con, past, rs);
) V" q& G/ ^$ G9 `8 a% Y - }
0 c. V: T Z7 ^4 C4 l- o - return list;- x% x2 V* x) R, F# Y: Z
- }- s6 I* M, E, A, K* Y
8 c1 a; I7 F5 A1 v- /**/ w7 ^( F* Z* P/ U
- * 多条件查询
% |: S; A$ s) p. y - */
% C( [; y! j5 P - public List SelectGame(Game game) {
5 t* H, p. I; y' `) c7 S - List list=new ArrayList(); //list 添加信息" k9 I$ m* N6 e9 u( N
- //sql 语句
7 |* I6 r' ?6 G - String sql="select * from game where 1=1";
, F) ]9 }$ w- K6 q. g1 T7 \) F - //连接数据库
* t1 V( `1 E& U9 _5 v+ \ - con=GameBase.GetConnection();
0 Y, o! F; T5 I - try {9 d1 l2 S; Y) C9 d2 k) m
-
; T- [" B; x3 y) @8 c5 r - //根据名称查询时" M9 w" U2 v7 u! u. d" V9 q: N2 p
- if(game.getGame_name()!=""){
( M7 R( A, `- I - sql=sql+"and game_name='"+game.getGame_name()+"'";
" p0 @- z8 D2 \0 E) n+ r -
1 g0 v9 T, Y. x2 W8 {8 x! d+ i - }; {4 c* z' ^& A9 v2 f* o# Z. D9 w
- //根据类型查询时! U6 l% \# y" c( l; j( T
- if (game.getGame_type()!="") {
0 V* s6 O( R% ^8 \' o - sql=sql+"and game_type='"+game.getGame_type()+"'";: G2 b4 n5 q. f5 ~* R
- }2 Q' O9 r( A* t ] z- |
- //根据公司名称查询时
* x2 o5 |7 a* K/ l! \ u( _ - if (game.getGame_company()!="") {
2 c) o) }& h8 N0 C" ?% b; Y - sql=sql+"and game_company='"+game.getGame_company()+"'";
4 M ?$ c* Q/ P, S2 _ W, ^ - }9 W; S2 F9 B: w: \! n/ l
- //根据年份查询时
3 J* q4 ?' T! q1 X+ T - if (game.getGame_year()!=0) {. k$ G2 M; `+ L2 y( b1 j8 j2 a. p. U
- sql=sql+"and game_year="+game.getGame_year();
; {8 S4 j4 m! _: {& Y6 c* I2 m - }4 A: b/ L; n5 Q9 I6 W
- //传入sql
5 E$ |! S! S# |$ Y! ?) F- [; o - past=con.prepareStatement(sql);, W! v: }; D% k7 n7 A+ w
- //执行sql/ k- Q0 a) q9 n& @
- rs=past.executeQuery();/ X& X- ?3 H, v# m, B+ o3 I# T, k7 K
- //读取信息
/ m _' j# d, z1 X2 u) B - while(rs.next()){
# T3 {2 g& X& j1 r" \8 T - int id=rs.getInt("game_id");8 H: W1 L" i3 @ B: ]. d& r7 s. Y! {
- //游戏名称: q& @% _5 M- t9 X" {) m; f j+ N
- String name=rs.getString("game_name");
7 k" N, I& b4 s+ k - //游戏类型
: \. N% Z W, X, [ l+ h - String type=rs.getString("game_type");1 V0 S- q% M9 b; V+ H
- //游戏发行公司5 \! S9 |7 R9 I5 g+ l5 X. ]6 ~
- String company=rs.getString("game_company");
. P' M. \/ [) q) |9 x# E - //游戏发行时间
1 h; c; \* w/ g" X1 W% h - int year=rs.getInt("game_year");
! y6 F$ g9 b. p8 F. X' }5 O - //添加到构造函数里
. n* H4 r' D) x - Game games=new Game(id,name,type,company,year);1 g4 w% c* n K# l3 Y1 H$ Y4 a
- //添加到list集合
/ ] ]6 t6 p& X8 B - list.add(games);
( ?/ J0 A4 i' n* f2 u m$ y4 N - - o( g1 S" ^. m8 P/ x, s
- }! w' F: H5 @' d" x; `% X ^1 g& x0 U
- } catch (SQLException e) {( k* A7 i2 F4 A9 C' k. S0 y
- System.out.println("SelectGame 方法出现异常");
" g6 K. G6 R7 l" G8 } - e.printStackTrace();
# ?2 J' y ]2 W Z$ e2 g, \ - }finally{
/ D5 \9 O( P: Z/ i! x9 M6 J - GameBase.colseAll(con, past, rs);) ^ m: E K& l! t9 Q1 W, a3 E1 j
- }6 X% U/ a1 \+ W/ t: D
- return list;
( S: X5 t! t, a [% c - }
3 M- h$ V5 v+ y( U0 F - }
1 E3 ~4 ^! S" l+ |2 d
复制代码- package s2.jsp.zhangxiao.entity;, \3 R5 c+ ] d
$ F% R9 }4 W& H/ l: H+ M2 W- /**
( A+ F* t3 J/ I4 } - * 游戏类
% A. z3 n8 ~9 {7 a0 R2 F - * @author Administrator+ ^, h% r; d! X. V* F! z; c6 C4 s4 u
- *
, a& t0 v& B6 {1 G - */- J( V& ^1 W. x, `* ^
- public class Game {
1 ?1 O0 \9 S9 ]+ o2 H, } -
+ M4 B# `+ r" H9 q( L5 \9 ]8 C( i4 K. e - private int game_id;
% W8 Z4 g; c5 N3 \3 f& d - private String game_name;
. B/ q3 U9 h$ S5 F9 q8 ?5 O - private String game_type;
5 v2 B/ f. P s$ ~1 ^5 @ - private String game_company;
: [% {* A+ j$ g# m+ Q2 R - private int game_year;) k: k% }" A! b& k! k
- /**6 a+ D, z$ v3 Q* ^ ~ ~8 k* d) J" G8 P
- * 有参构造函数9 p& k" f. c1 Y0 o5 }
- * @param game_company
* D+ ^) i4 a3 m% D/ j: I - * @param game_id
+ _+ h7 |8 _/ n - * @param game_name8 h9 H; i; G5 t6 @( b" o- U1 j
- * @param game_type' \$ L+ d9 A( o4 p- X3 t
- * @param game_year- H ]/ k' S3 j1 [
- */, u7 }3 q: C- a' I( Q
- public Game( int game_id, String game_name,% h5 c. H1 [4 `& t6 R, L
- String game_type,String game_company, int game_year) {
7 p( n0 b8 U( C8 n- }4 ^( s: ` - super();0 S4 l1 r0 C& a( N4 r4 Q3 l
- this.game_company = game_company;* ~( R- _; A/ V! w' L9 M5 ?. l
- this.game_id = game_id;: p- r5 J0 k0 I% [" B7 {5 h
- this.game_name = game_name;6 i C) \. M1 K, U1 H7 p! c3 Z u
- this.game_type = game_type;# H2 E" k$ Y* K% f7 r
- this.game_year = game_year;3 Y6 M. G% @ m5 K/ v4 K4 ~" g4 ?5 k
- }% H, F1 U, N6 r2 p% `1 o
- /*** `3 b. B9 p0 I3 i& g! a6 A7 O; b
- * 无惨构造函数
: u6 E- X4 }) j3 r! v, L4 e - */% ^* X4 M4 G0 ]& y( V& R1 R
- public Game()* @* ?* m5 w9 k# O# t
- {
: _# b6 \7 s, i, @: I* D -
$ n) E4 d2 v' b* K4 b6 S - }
! r: g8 g! [) h; F% i7 s - /**
; a- v8 L1 ^; D2 O; E - * 游戏编号0 ~& P% U9 {/ |: N5 @
- * @return" k* m( c3 Q2 a( g2 @
- */
4 d6 K i- o" C* c$ H% s, j1 H - public int getGame_id() {
7 K$ b* q- z/ `6 v - return game_id;
y4 C1 }+ K3 r - }! ^6 t/ D; D. _ X. x1 _, ^& S
- /**( i/ w' j2 Y6 i. H$ A
- * 游戏编号4 v8 k0 P9 B8 G$ W% C6 R0 Y, M
- * @return3 }7 R# B! m% X
- */
4 \3 }" Y4 E! X3 E - public void setGame_id(int game_id) { ]2 H$ @+ X5 n8 I& m- P
- this.game_id = game_id;$ k% I3 w1 l+ p a# D- Q4 S: Y
- }" n7 z6 H& x* o4 O; W! r/ m
- /**. S; g# u" n8 l; }
- * 游戏名称) s/ ]. a6 N7 F. u6 s! `
- * @return
2 U4 b' L) V$ Q& g6 Q8 d. C& X - */
( k' L5 g6 M1 o7 D( e5 ?9 ~# S - public String getGame_name() {1 g! I }9 h& ~1 H9 ]1 n" I
- return game_name;
6 O0 B( K8 O# P$ E2 e6 r - }
* T( E2 B# a/ U# s5 u! T - /** u, \* k2 F8 J( G( ?
- * 游戏名称
+ M* \% w3 @& Q- r1 ~ - * @return. l! Z. a# F4 F, h( o% n2 q
- */8 c! V9 S2 @6 ^' j" u& ^2 {
- public void setGame_name(String game_name) {
: A( | t0 x# ?- R' ]9 O - this.game_name = game_name;
; t/ j/ `# `7 k) J7 a t& E - }
6 {& B6 d; d: `. |& U& o! [8 u2 P - /**; `4 }9 n. X f3 T2 o0 M, o' y+ m
- * 游戏类型( X8 I9 Y, U, f9 a9 v
- * @return
2 A- ~7 c. L0 r3 _/ A! m; i: W - */
- w P; _# S: b0 u - public String getGame_type() {
: D$ u; P& q J: r: U% M$ l1 J - return game_type;
2 m3 P& n' {8 d - } ^* o; D" r+ n" v
- /**
0 c: I6 r" d r$ e% p( F - * 游戏类型
+ ]6 q* E- ~4 P$ i" q4 |, a, q. Q2 v - * @return1 |5 b$ D4 s, }$ [
- */
' Z! w D {* @- N# g6 a; v2 \' h - public void setGame_type(String game_type) {' u7 c0 P K7 F' B4 B" w
- this.game_type = game_type;
# A; M- \1 V( M# p) {4 d- J& h - }6 E0 i Z4 R' o& w X
- /**
3 [+ x/ w& \2 Z' `2 u$ j% I - * 发行公司7 i1 y3 g6 [2 ~' B
- * @return; K( K" Z# H! z$ @& ~
- */6 P8 X+ `0 b# o5 @
- public String getGame_company() {5 E( B) O& F* o4 Q2 N* {
- return game_company;
5 V7 |2 R5 g% ?) r2 a& j" Z - }! h" w3 x) N9 x5 _% L8 O, i& D+ Y% {
- /**0 ^5 j- |, N) y9 g
- * 发行公司
8 v8 u2 b4 d6 ^9 I- k) N7 ? - * @return1 N# z! f/ g3 G8 Y
- */
1 v" g8 j0 M% s- C! l( U - public void setGame_company(String game_company) {3 @% p! X; g* J' _( M C
- this.game_company = game_company;
6 ~! o. p! z. W" y7 @ - }! L. A) F' O, E2 E& q1 I* {5 h
- /**& b; Y& b4 _) Y1 k
- * 发行年份5 v! W% } a, A. I1 p. n1 \. o
- * @return* ^4 l: t) \$ ]) Y
- */
7 p& ~9 O; A* s( ^$ p$ d# T - public int getGame_year() {8 D# K- `/ e$ f6 I, L5 W. ^
- return game_year;
% l, K1 D& {! e; ]% p* o - }5 n ]: d+ X. J9 r
- /**
6 t9 b- |, h: ?- ~* j0 a) g5 N - * 发行年份
- ?% d/ M% b7 O1 w - * @return( B; [% M2 ?. u4 p' T/ e2 \
- */& l) X7 f7 s# e
- public void setGame_year(int game_year) { v5 `! f; l: x8 F1 s: j' v! m4 U7 F
- this.game_year = game_year;
2 N' `& V# P) ]1 t( K - }
" E$ M4 r+ o& ?' n, H, l - }
- H$ ]5 ^9 l! B3 d
复制代码
( ]7 `! v5 C, `! T% }+ n- D" h: n7 f
6 D$ N9 b, X: K$ Q; h& j2 k4 R* o" C/ s* P
|
|