排球比赛(第一次迭代)
用户故事:作为一个排球比赛的观众,我希望获得一场比赛的得分,以便了解比赛的结果。
由用户故事拆成任务:
1.根据观众要求,显示某一场比赛的局分;(表现)
2.记分员输入每一局比赛的分数,并保存;(表现)
3.根据局分判断输赢;
4.连接数据库保存局分。
根据用户故事,创建以下包,类。
util包:做数据连接。
DBUtil.java类:
package com.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBUtil {
public static Connection getConn(){
Connection conn=null;
try {
//加载驱动
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//连接数据库 conn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=MySqlGames","sa","sa");
}
catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.println("驱动加载失败");
} catch (SQLException e) {
e.printStackTrace();
System.out.println("数据库连接失败");
}
return conn;
}
//释放资源
public static void closeAll(Connection conn,Statement statement,ResultSet rs){
try {
if(rs!=null){
rs.close();
}
if(statement!=null){
statement.close();
}
if(conn!=null){
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
System.out.println("释放资源出现异常");
}
}
}
User包下:bean包,和dao包:
bean 包下GamesInfo类(get,set):
package com.user.bean;
public class GamesInfo {
private int gID;//比赛场次
private String gTeam1;//队伍1
private int gOne;//第一局
private int gTwo;
private int gThree;
private int gFour;
private int gFive;
private int gResult1;//队伍1结果
private String gTeam2;//队伍2
private int gResult2; //队伍2结果
/**
* 构造方法
* */
public GamesInfo() {
super();
}
public GamesInfo(int gID, String gTeam1, int gOne, int gTwo, int gThree,
int gFour, int gFive, int gResult1, String gTeam2, int gResult2) {
super();
this.gID = gID;
this.gTeam1 = gTeam1;
this.gOne = gOne;
this.gTwo = gTwo;
this.gThree = gThree;
this.gFour = gFour;
this.gFive = gFive;
this.gResult1 = gResult1;
this.gTeam2 = gTeam2;
this.gResult2 = gResult2;
}
public int getgID() {
return gID;
}
public void setgID(int gID) {
this.gID = gID;
}
public String getgTeam1() {
return gTeam1;
}
public void setgTeam1(String gTeam1) {
this.gTeam1 = gTeam1;
}
public int getgOne() {
return gOne;
}
public void setgOne(int gOne) {
this.gOne = gOne;
}
public int getgTwo() {
return gTwo;
}
public void setgTwo(int gTwo) {
this.gTwo = gTwo;
}
public int getgThree() {
return gThree;
}
public void setgThree(int gThree) {
this.gThree = gThree;
}
public int getgFour() {
return gFour;
}
public void setgFour(int gFour) {
this.gFour = gFour;
}
public int getgFive() {
return gFive;
}
public void setgFive(int gFive) {
this.gFive = gFive;
}
public int getgResult1() {
return gResult1;
}
public void setgResult1(int gResult1) {
this.gResult1 = gResult1;
}
public String getgTeam2() {
return gTeam2;
}
public void setgTeam2(String gTeam2) {
this.gTeam2 = gTeam2;
}
public int getgResult2() {
return gResult2;
}
public void setgResult2(int gResult2) {
this.gResult2 = gResult2;
}
}
Dao包下,接口类GamesInfoDao和GamesInfoDaoImpl包:
GamesInfoDao.Java类:
package com.user.dao;
import com.user.bean.GamesInfo;
public interface GamesInfoDao {
/**
* 接口
* */
//添加比赛
boolean insertGame(GamesInfo gamesInfo);
//根据场次查询比赛
GamesInfo getgameByID(int id);
}
GamesInfoDaoImpl包下GamesInfoDaoImpl接口实现类继承DBUtil
GamesInfoDaoImpl.java类:
package com.user.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.user.bean.GamesInfo;
import com.user.dao.GamesInfoDao;
import com.util.DBUtil;
/**
* 接口实现类
* */
public class GamesInfoDaoImpl extends DBUtil implements GamesInfoDao {
//根据场次查询比赛
public GamesInfo getgameByID(int id) {
GamesInfo game=null;
Connection conn=null;
PreparedStatement pstatement=null;
ResultSet rs=null;
try {
conn=getConn();
String sql="select *from games where gID=?";
pstatement=conn.prepareStatement(sql);
pstatement.setInt(1, id);
rs=pstatement.executeQuery();
if(rs.next()){
int gID=rs.getInt("gID");
String gTeam1=rs.getString("gTeam1");
int gOne=rs.getInt("gOne");
int gTwo=rs.getInt("gTwo");
int gThree=rs.getInt("gThree");
int gFour=rs.getInt("gFour");
int gFive=rs.getInt("gFive");
int gResult1=rs.getInt("gResult1");
String gTeam2=rs.getString("gTeam2");
int gResult2=rs.getInt("gResult2");
game=new GamesInfo(gID, gTeam1, gOne, gTwo, gThree,gFour,gFive,gResult1,gTeam2,gResult2);
}
//抛出异常
} catch (SQLException e) {
e.printStackTrace();
}finally{
closeAll(conn, pstatement, rs);//释放资源
}
return game;
}
//添加比赛
public boolean insertGame(GamesInfo gamesInfo) {
boolean flag=false;
Connection conn=null;
PreparedStatement pstatement=null;
try{
conn=getConn();
String sql="set identity_insert games ON;insert into games(gID,gTeam1,gOne,gTwo,gThree,gFour,gFive,gResult1,gTeam2,gResult2) values(?,?,?,?,?,?,?,?,?,?)";
pstatement = conn.prepareStatement(sql);
pstatement.setInt(1, gamesInfo.getgID());
pstatement.setString(2, gamesInfo.getgTeam1());
pstatement.setInt(3, gamesInfo.getgOne());
pstatement.setInt(4, gamesInfo.getgTwo());
pstatement.setInt(5, gamesInfo.getgThree());
pstatement.setInt(6, gamesInfo.getgFour());
pstatement.setInt(7, gamesInfo.getgFive());
pstatement.setInt(8, gamesInfo.getgResult1());
pstatement.setString(9, gamesInfo.getgTeam2());
pstatement.setInt(10, gamesInfo.getgResult2());
int rowNum=pstatement.executeUpdate();
if(rowNum==1){
flag=true;
}
//抛出异常
}catch (SQLException e) {
e.printStackTrace();
}finally{
closeAll(conn, pstatement, null);//释放资源
}
return flag;
}
}
Test包:表现层
TestGetGameByID类:用户根据id场次查询结果:
package com.test;
import java.util.Scanner;
import com.user.bean.GamesInfo;
import com.user.dao.GamesInfoDao;
import com.user.dao.impl.GamesInfoDaoImpl;
public class TestGetGameByID {
public static void main(String[] args) {
/**
* 用户查询比赛
* */
GamesInfoDao gamesInfoDao=new GamesInfoDaoImpl();//实例化接口实现类
Scanner input=new Scanner(System.in);//输入实现类
System.out.println("请输入比赛场次:");
int RA=input.nextInt();//接收用户输入查询场次
GamesInfo gamesInfo=gamesInfoDao.getgameByID(RA);//查询场次为RA的比赛详细信息
System.out.println("比赛场次:"+gamesInfo.getgID());
int one=gamesInfo.getgOne();
int two=gamesInfo.getgTwo();
int three=gamesInfo.getgThree();
int four=gamesInfo.getgFour();
int five=gamesInfo.getgFive();
if(one+two+three+four+five>3){
System.out.println(gamesInfo.getgTeam1()+":胜利");
System.out.println(gamesInfo.getgTeam2()+":失败");
}else{
System.out.println(gamesInfo.getgTeam1()+":失败");
System.out.println(gamesInfo.getgTeam2()+":胜利");
}
}
}
运行效果图如下:
测试数据为:比赛场次为1;
队伍一名称为:1;
队伍二名称为:1;(随便写入的,这数据有点尴尬 = - =)
TestInsertGame.Java类,记分员写入比赛数据:
package com.test;
import java.util.Scanner;
import com.user.bean.GamesInfo;
import com.user.dao.GamesInfoDao;
import com.user.dao.impl.GamesInfoDaoImpl;
public class TestInsertGame {
public static void main(String[] args) {
GamesInfoDao gamesInfoDao=new GamesInfoDaoImpl();//实例化接口实现类
/**
*
* 接收教练输入比赛信息
* */
Scanner input=new Scanner(System.in);
System.out.println("请输入比赛场次:");
int RA=input.nextInt();
System.out.println("请输入第一队伍名称");
String Team1=input.next();
System.out.println("请输入第二队伍名称");
String Team2=input.next();
int[] game=new int[5];
int sum=0;
int gResult1=0;
int gResult2=1;
for(int i=0;i<5;i++){
System.out.println("请输入第"+(i+1)+"局"+Team1+"胜负情况。1/0(胜/负):");
game[i]=input.nextInt();
sum=sum+game[i];
if(sum>=3){
gResult1=1;
gResult2=0;
game[3]=0;
game[4]=0;
break;
}
}
GamesInfo gamesInfo=new GamesInfo(RA,Team1,game[0],game[1],game[2],game[3],game[4],gResult1,Team2,gResult2);
boolean flag=gamesInfoDao.insertGame(gamesInfo);//执行添加
if(gResult1==1){
System.out.println(Team1+"胜利");
}else{
System.out.println(Team2+"胜利");
}
if(flag){
System.out.println("添加成功");
}else{
System.out.println("添加失败");
}
}
}
运行效果图:
数据库数据添加成功:
到此功能就完成的差不多了,接下来说说要注意的事项,以及在过程中遇到的问题:
1.数据库连接驱动jar包,不要忘了提前准备好,放在WEB-INF的lib下,myeclipse会自动加载到上面。
2.配置环境变量。不然数据库还是连不上(刚装系统的我已尝试过了 >=< ),这个随后我要写个傻瓜式操作。
遇到的问题:
1.有图有真相
在写入数据库时不让写主键标识列
解决办法:
在写SQL语句时把它设为ON
2.本以为要过的时候报错。(已搬个小马扎去反思 ==||)
第五个总是过不去,判断都去了还是不行,最后找到了:
看到没,数组下标是从0开始的。
3.还有一个没有解决的问题:
数据库是硬伤:来看看我建的表就知道了
能看懂吗,连我自己都凌乱了。我知道这个肯定不行,仅限于这第一次能用,看了这么多,还希望各位小主给点建议and意见。
总结下:我觉得最后表现层写的比较乱,都写在test里了,应该在写个逻辑工具类,这样关系应该更加明了了(就是传说中的高内聚低耦合吧)。
总体下来思考了好几天(更多是做其他的了),最后否了自己的空想云云,按步骤下来,花了我一个午饭和一个晚饭的时间写下来。
哦,no,还有我的现在,晚安。