该用户从未签到
|
板凳
发表于 2014-04-07 17:50:10
|只看该作者
- package s2.jsp.zhangxiao.daoImpl;
3 _* \1 w) ~. M M z
0 `0 U; B5 e- Y- import java.sql.Connection;
( f& Y/ A2 ^# P+ I& N2 T. L - import java.sql.PreparedStatement;
0 }1 ?5 H0 q6 ]& `0 V - import java.sql.ResultSet;
% z. k( @( e6 H3 x - import java.sql.SQLException;- F. ^' X( g& i& D* Q1 }: C
- import java.util.ArrayList;
$ O2 R1 r, _ c5 h - import java.util.List;& U# i1 a5 r2 O, Q+ ~1 ^
" E7 r# P" b& C" L, n! K3 J' o- import s2.jsp.zhangxiao.dao.GameBase;. Z5 F: _4 Y6 y* z
- import s2.jsp.zhangxiao.dao.GameDao;/ j) N. d( K+ H1 F$ C
- import s2.jsp.zhangxiao.entity.Game;2 o6 R# v0 a: n; t6 P1 p
- 4 E" C1 r( b% t; d3 S
- 2 m t7 h; B9 ^: h# z
- public class GameImpl implements GameDao {
' u" ]. S. M, h' m -
) {8 {7 T7 D; o - Connection con=null; //数据库连接* p0 T3 K. H( B* p
- PreparedStatement past=null; //执行sql语句+ @( R- S$ P! t# L
- ResultSet rs=null; //结果集& W# m, L# A& h8 A% ~
- List list=new ArrayList(); //list 添加信息
' o, d4 |' m4 J" @7 l' M) ^ - Game game=null; //声明Game类
, E% S/ O' d* l -
* o0 W" y- w/ N" E9 \ - /**4 Y8 J3 Q0 r" s0 V! W
- * 实现增加方法* Q O8 c9 @/ C+ e0 F% @
- * @author Administrator
0 Y7 r8 _* r, f4 Q E, q A: N - *
6 M! P! A9 g1 W, j G$ R - */
! I( X' r0 F2 {% e- M - public int GameInsert(Game game) {( U3 | I, o- {+ p; K" G6 S1 f
- //sql 语句
. S4 Z; L+ u, P0 u - String sql="insert into game values(?,?,?,?)";4 t( Q. B9 c. d6 T; ~# l7 F6 U2 O
- //添加信息
* Z: p6 ?7 q3 a) w: `* F - String [] getValues={game.getGame_name(),game.getGame_type(),game.getGame_company(),game.getGame_year()+""};
`7 w9 B( R# i2 p) b( x( i - //调用数据库连接方法
t* m. V# Z- ^4 K/ z) ]9 w - return GameBase.executeUpdate(sql, getValues);
7 J. d. ~6 r; ]7 v6 C - }
3 K. r8 v$ ?* h- N" q, Y; M$ B: C - /**# K/ K2 I' g+ R$ t F% O) f, A
- * 查询所有游戏信息
r b1 c9 O9 q# B* d [ - */
- ~% G5 Z) E2 `: O - public List GameCount() {
( Z6 `, c" o% q, f1 @ - String sql="select * from game";; F( \* ]9 q/ s' T" @+ ?) C9 P: x
- //连接数据库、
, @( I3 m( C5 c; ? - con=GameBase.GetConnection();
- q, P7 z# A& P1 O, Y1 Y* a - //传入sql 语句: T0 @/ ?/ `2 ~. |0 `; [: ~- H
- try {
$ l. R" J0 j" n* {6 L9 e - past=con.prepareStatement(sql);- b+ L, C8 @- `) t6 Z5 D4 o- }
- rs=past.executeQuery();' x& z- J, [8 q
- //读取信息4 y8 b" C0 c# C A; y) e
- while(rs.next())& v8 ]4 p! {0 B7 C
- {- d) o( Y1 y! Q: `; `
- int id=rs.getInt("game_id");/ F6 X* S @) v$ F7 C: Q
- //游戏名称1 W% q/ c5 U7 o) b" T* t- r
- String name=rs.getString("game_name");
0 H) c' L0 `6 T K - //游戏类型- K' e# q. Z9 Q9 C
- String type=rs.getString("game_type");
( y1 N; i& b) s( @6 c - //游戏发行公司' j) t6 I; s" u+ H. w, C( b
- String company=rs.getString("game_company");% U* K8 n/ J" Y
- //游戏发行时间
& E% {% l) T x# X% j - int year=rs.getInt("game_year");& v0 u) Q. s; P& \/ ?) _" T
- //添加到构造函数里
& s x8 Y- Y Z. @ L3 h+ D6 X3 S" B7 M - Game game=new Game(id,name,type,company,year);
X8 I( a; H: t7 n - //添加到list集合9 w$ {2 Z* h, p: k5 @
- list.add(game);
; z- U/ g1 t4 K; D% [3 c - }
& O) B+ \0 z, h3 e# v7 ` - } catch (SQLException e) {# f& K2 v* o2 W! O
- System.out.println("GameCount 方法出现异常");- m0 u1 q$ K- D$ p0 }, L" N
- e.printStackTrace();9 x, f1 Y8 D, t
- }finally5 c ]2 x, @* x) f/ w& N
- {
) A+ {8 S/ ]& K1 J& q2 H# | - GameBase.colseAll(con, past, rs);
; f1 E: b4 e8 @ - }
: g$ R- {/ Z7 m) a6 j5 S. J - return list;# L% W( f' T' H! V
- }
- _& V8 q% F7 p5 k1 A6 a
5 A$ A* m8 B' T5 i0 T- /**
9 n- H% i+ L4 N; H U! G - * 多条件查询
3 V- ~2 m+ s) K8 U - */9 y/ h: I* s q z5 d j0 N# C- e
- public List SelectGame(Game game) {: o1 f0 ^# B. K- t7 k6 }
- List list=new ArrayList(); //list 添加信息
- L1 P9 |5 J) {3 m1 X/ M - //sql 语句" F* |/ Z% w! V. i& ^# z
- String sql="select * from game where 1=1";
+ o( n& J- L; D% L) }( J - //连接数据库
8 M$ z( H6 l: y( F% ]. `0 v+ [ - con=GameBase.GetConnection();
! e7 K& j/ B: r) [& Z: f8 N - try {
' q) r: H3 {& [! T -
/ p d R# i* o7 ^- g - //根据名称查询时# Q, e: [+ U2 p. d$ z8 d' Y. m" G
- if(game.getGame_name()!=""){8 i( i0 ~4 A5 M: y
- sql=sql+"and game_name='"+game.getGame_name()+"'";' y1 W. K& R" V% w6 |
- # [0 M1 ]# c7 L1 M# u8 p1 U
- }0 [0 k5 } L) h4 H! X
- //根据类型查询时+ @) [! I7 }2 L: z/ ?; t' X
- if (game.getGame_type()!="") {
& A" s! u! {4 J2 s: V& K - sql=sql+"and game_type='"+game.getGame_type()+"'";
1 {2 n6 u4 L+ V. ?4 z* c - }3 r2 Q* |# V0 ]' h: K$ Z
- //根据公司名称查询时6 f: y( `6 i' b" [) s/ n
- if (game.getGame_company()!="") {/ P( [! n7 C, z: A! a* k
- sql=sql+"and game_company='"+game.getGame_company()+"'";
) B" c. k0 @8 v- R, Q. l - }, H9 f! I) a5 J3 y& P B0 K4 b8 E) t
- //根据年份查询时% ]/ W9 Q3 n- g% }2 T6 k
- if (game.getGame_year()!=0) {- Q0 ? w- t2 V' M5 R- i
- sql=sql+"and game_year="+game.getGame_year();
0 j6 K- H1 D* c) C9 ^ - }
; t6 i3 O" p% ~ - //传入sql
! V9 q' U7 `4 o$ t" m+ k - past=con.prepareStatement(sql);
8 y b5 A2 e4 t$ x4 b) p - //执行sql
, h% q& }: Z( ? - rs=past.executeQuery();
. S( N7 ?) U; X& [- g& Y: ` - //读取信息+ X$ U3 k" u z) F: ?- O, i/ r9 }
- while(rs.next()){8 [5 n2 x: K. H, W$ x9 T6 p! T
- int id=rs.getInt("game_id");
" Y( ^; s$ m# @ - //游戏名称
) o+ t% W' B$ U0 q) r - String name=rs.getString("game_name");
/ H; Y8 _, N6 ?- H. V3 I# w - //游戏类型, w5 g Z( ]% b+ H) J3 l/ f% d
- String type=rs.getString("game_type");
$ r$ R& g1 v' M0 n7 `8 b: l: O - //游戏发行公司8 s+ ~- |1 z \) n1 R8 ~7 z
- String company=rs.getString("game_company");
+ T% d. V: O3 m! ]" g7 p. o2 J" ^ - //游戏发行时间# y6 t, B$ E5 S$ } U) N3 I( s
- int year=rs.getInt("game_year");' j% x, Q. X1 A' W3 E, ?
- //添加到构造函数里$ h2 A1 \* S0 ?; Q+ g# L" U9 v$ ]
- Game games=new Game(id,name,type,company,year);/ m, V. i4 g! Y/ z0 p; V
- //添加到list集合% x* M& S1 ^4 J+ n2 b; r% P1 }
- list.add(games);9 U& P2 B4 y- c. R2 l# l# B
-
2 J5 Z1 L* J4 u, H, ~" x - }3 E3 L) q# [: m# B6 u0 ~7 `9 W0 G
- } catch (SQLException e) {' E+ h# b& ^9 L4 H$ l3 o* U
- System.out.println("SelectGame 方法出现异常");
# _# `! [) P8 G+ a; }, x - e.printStackTrace();
; u( j) P7 H9 S' i# J$ U" K i - }finally{
' G$ ]) i' u5 u; h" F) x3 d/ g - GameBase.colseAll(con, past, rs);
" m2 `' D- M8 l. m7 S/ l y/ W - }+ n* w, `% B" \# c- ~+ r( j
- return list;
7 R3 p7 I- g( F2 W& [ - }6 X8 E5 ]+ ^% m2 v7 U8 I- h4 ^# Z
- }
" M1 a( q( w; ]. J5 d
复制代码- package s2.jsp.zhangxiao.entity;
2 h; H' m, x+ H8 V! B - 2 V$ U0 T5 U; M8 `4 ?6 E2 M7 F& }
- /**! D' j, @$ e3 K2 t
- * 游戏类) o d$ }+ X1 d" s) E
- * @author Administrator" v7 {; t" \" X0 `
- *
& N9 H! G4 _6 \4 x, J) x - */
4 @3 b6 j% E) R! z$ N$ _ - public class Game {
' B" ~3 s0 C0 g, E, p+ v! y3 e - 6 q& f4 \9 p& x
- private int game_id;
4 s) F1 y! B( Z0 L) X - private String game_name;2 b* Y* A9 W8 I2 r a" n1 d8 W- q
- private String game_type;# P% R7 C, @" T# U4 O
- private String game_company;0 W( x. ?* v- ~- I `# N
- private int game_year;
1 h, K# N" n! B7 a! o# n8 ` - /**
6 d9 | w0 k% a0 K- @ - * 有参构造函数
3 X# K7 T# X4 t+ i N/ I3 \ - * @param game_company; p, P, r. o: M& j
- * @param game_id
% [# K$ c9 s2 q' l" R% S - * @param game_name
+ m9 R1 h: T& Q; Q' l+ Z - * @param game_type! Y" k5 Z- [2 O7 i2 Z+ T; a- I
- * @param game_year9 H+ L+ A( I- w; r
- */
# ?% E/ {7 `* n5 L5 E - public Game( int game_id, String game_name,6 O6 [0 T$ V# k
- String game_type,String game_company, int game_year) {
: F) L2 B: D. h7 P - super();' L) ?. Z, _4 P
- this.game_company = game_company;3 ~- I) p! |: h% w: y. C* Q
- this.game_id = game_id;
& e9 M1 y/ h6 P# o7 d - this.game_name = game_name;5 Z6 H: i( v* `: t; I
- this.game_type = game_type;& a0 E3 E4 K0 q- Z& X& X# _
- this.game_year = game_year;
1 U# _0 V. K% V% h - }
/ d$ A6 t- D* s8 h - /**/ F% g3 K) D4 J* V% P$ K
- * 无惨构造函数5 G; p: w- K2 C( H( P5 d: h
- */
9 Y: m# L+ O L+ ~" X - public Game()
- r, M1 L: \. ~' W* o - {
, A* B! u) d' h0 C* s, n - " x% d- X* o3 T* d% B. {4 A
- }
5 {/ K9 F# o( p - /**! J5 j: W4 h0 H7 K- R' h3 B
- * 游戏编号7 x1 T4 |/ C c
- * @return
5 r) l* }) n+ ~( p( L - */
2 R: Y7 H0 b, p4 B- w2 P& h! y5 K T - public int getGame_id() {9 B! C( \) R% D) u1 `+ _: t
- return game_id;
1 J. A+ W3 [7 E- n* z: C! ~2 A - }0 ^1 q1 Y9 ~" ~8 V: {
- /** v: W: B6 q, f; \ ?, e
- * 游戏编号
" n5 K( K+ t* T' n$ G! ?8 y- _7 G - * @return
' F5 S# G. E! H - */. M7 {+ ]! g$ r, I% B" N. D- E
- public void setGame_id(int game_id) {
2 [$ L3 L V% d - this.game_id = game_id;
+ O. U+ W, ]: u; y' h1 L' g - }$ X, Q/ ~+ y: J; j9 ]9 i& i
- /**1 ~) t: R( M8 J7 G$ L2 }* S3 U0 |
- * 游戏名称1 \! w6 D, v, @ c% U* M
- * @return
. [1 ^+ n7 _' Q1 n8 ]( i. `1 S - */
; P b S# s8 i) C: z4 S) ] - public String getGame_name() {) X( g" h3 T" k, ]- }3 ]& a
- return game_name;
/ G, h) ?+ f% L& Y - }
. ?& x B& H, u+ u3 j6 W, ~ - /**
( Z; T( I. E0 L* n6 M7 v - * 游戏名称
( D9 P" I% [1 b2 N9 c5 R# i! E& s - * @return
" a$ I( {* Z: b5 ?/ _ - */
7 X/ k9 G- H) o w( d - public void setGame_name(String game_name) {
* B/ j" v3 q5 i$ |& |7 l - this.game_name = game_name;9 Z& h' f+ ^5 l$ U
- }
& l9 Y* ]2 b" A' q" O$ F- x - /**- Z: ^! E* S$ l9 D
- * 游戏类型
2 C/ S e% I1 j4 e+ ~% y* j5 s$ P - * @return$ s* E k# g8 t2 C3 E
- */; \# G& Q0 U0 Q
- public String getGame_type() {7 C" K! h5 H1 c+ b
- return game_type;
2 V4 L3 T1 e: E& o6 ]! ]% `* I3 m0 X - }
0 A; b2 [- a# g3 [0 c - /**5 A ~( G/ q) s: v t( W
- * 游戏类型
9 y/ c P1 c& ?$ T. k0 X - * @return$ z3 T) J4 I) }: c* f
- */6 i. I: x9 }+ u" F0 F4 O( \' X: m
- public void setGame_type(String game_type) {$ L+ \" {/ ~, q% M8 q0 q
- this.game_type = game_type;
`8 y2 x2 u m* v0 @ - }. o: U2 i. E* F8 i
- /**& J! G; c; t9 `% ]' v
- * 发行公司
- Q5 c1 T! I4 Y) h - * @return
" `- P: ]9 u* {: h* y: E' X3 b - */* L0 f1 o1 {9 _. a$ V6 y) [
- public String getGame_company() { F" P( ~& ^* B( [
- return game_company;3 @# G* {8 D% W* y) g
- }
# E/ }0 Q- B( Y - /**. @/ [6 f7 i/ ~$ I$ r
- * 发行公司
9 g( r' s( t1 |7 k9 ]' ]. o - * @return F% H9 a! a% L0 g$ A" h9 K
- */ r" m) p) `5 J6 I
- public void setGame_company(String game_company) {( X' E; u6 e7 Z, U- y V
- this.game_company = game_company;6 q$ G2 F/ x9 i$ U# T
- }. |3 Q% m8 G/ h4 Q
- /**
6 P8 T5 U5 Y+ Q2 ^0 B2 f" P - * 发行年份
, f# I4 ~. x5 g" [( Y5 @; b* d - * @return
0 d7 _ E8 v$ _& Q) z - */" O/ K1 P4 O4 n
- public int getGame_year() {1 u- t4 ]0 R+ s, c5 @* ]
- return game_year;) V# r5 _$ Z! h" E |1 h- }2 W
- }/ H# s6 _6 F9 w/ N; u
- /**; J0 M/ g; _3 t' F+ j
- * 发行年份
) _1 |. b& D% f1 n0 T+ Q- f - * @return2 B+ i: {; d, C* M& o1 J( [
- */) _' f1 f8 j. {9 ^% \: G
- public void setGame_year(int game_year) {
' G7 ]0 ~3 e: ?2 _4 W* l - this.game_year = game_year;
' Z8 g, u# t4 G! M# ]! E5 Y$ j - }
2 j, _/ d5 c0 A& }! y - }
) ]' D1 O F( m) Z
复制代码 ; A3 P, w* o4 t: P+ {
7 p; w8 ?6 k' p$ ]! e+ q
! P J2 E" h a, k2 E0 `) ^ |
|