游戏管理系统源码
package s2.jsp.zhangxiao.dao;import java.sql.PreparedStatement;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.microsoft.sqlserver.jdbc.SQLServerDriver;
/**
* 访问数据库
* @author Administrator
*
*/
public class Base {
private static final String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static final String URL="jdbc:sqlserver://localhost:1433;DataBaseName=messageboard";
private static final String DBNAME="sa";
private static final String DBPASS="1234";
/**
* 连接数据库
*/
public static Connection getConnection()
{
Connection con=null;
try {
Class.forName(DRIVER);
con=DriverManager.getConnection(URL, DBNAME, DBPASS);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}
/**
* 关闭数据库
*/
public void closeALl(Connection con,PreparedStatement past,ResultSet rs){
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
past.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}<div class="blockcode"><blockquote>package s2.jsp.zhangxiao.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.sound.midi.SysexMessage;
import sun.security.action.GetBooleanAction;
/**
* 连接数据库库
* @author Administrator
*
*/
public class GameBase {
private static final String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static final String URL = "jdbc:sqlserver://localhost:1433;DataBaseName=games";
private static final String DBNAME = "sa";
private static final String DBPASS = "1234";
/**
* 打开数据库
*/
public static Connection GetConnection()
{
Connection con=null;
try{
Class.forName(DRIVER);
con=DriverManager.getConnection(URL, DBNAME, DBPASS);
}catch(ClassNotFoundException e){
System.out.println("加载驱动出现异常");
}catch(SQLException e){
System.out.println("数据库连接出现异常");
e.printStackTrace();
}
return con;
}
/**
* 关闭数据库
* @param con
* @param past
* @param rs
*/
public static void colseAll(Connection con ,PreparedStatement past,ResultSet rs){
if (rs!=null) {
try {
rs.close();
} catch (SQLException e) {
System.out.println("rs关闭出现异常");
}
}
if (past!=null) {
try {
past.close();
} catch (SQLException e) {
System.out.println("past关闭出现异常");
}
}
if (con!=null) {
try {
con.close();
} catch (SQLException e) {
System.out.println("con关闭出现异常");
}
}
}
/**
* 数据的 增 删 改
* @param con
*/
public static int executeUpdate(String sql,String[] getValues){
int t=0;
PreparedStatement past=null;
Connection con=null;
try {
con=GetConnection();
past=con.prepareStatement(sql);
if (getValues!=null) {
for (int i = 0; i < getValues.length; i++) {
past.setString(i+1,getValues);
}
}
t=past.executeUpdate();
} catch (SQLException e) {
System.out.println("执行数据库操作出现异常");
e.printStackTrace();
}finally{
colseAll(con,past,null);
}
return t;
}
/**
* 测试连接
* @param args
*/
public static void main(String[] args) {
Connection conn=GetConnection();
if(conn!=null){
System.out.println("连接成功");
}else{
System.out.println("连接失败");
}
}
}
}
/**
* 增删改
*/
public int execuUpdate(String sql,String[] getValues){
int i=0;
Connection con =null;
PreparedStatementpast=null;
con=getConnection();
try {
past=con.prepareStatement(sql);
if(getValues!=null){
for (int j = 0; j < getValues.length; j++) {
past.setString(j+1, getValues);
}
}
i= past.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return i;
}
public static void main(String []args){
Connection con=getConnection();
if(con!=null){
System.out.println("成功");
}
}
}
源代码下载地址 :点击下载
package s2.jsp.zhangxiao.dao;
import java.util.List;
import s2.jsp.zhangxiao.entity.*;
public interface GameDao {
//增加游戏信息
public int GameInsert(Game game);
//查询游戏信息
public List SelectGame(Game game);
//查询所有游戏信息
public List GameCount();
}
package s2.jsp.zhangxiao.daoImpl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import s2.jsp.zhangxiao.dao.GameBase;
import s2.jsp.zhangxiao.dao.GameDao;
import s2.jsp.zhangxiao.entity.Game;
public class GameImpl implements GameDao {
Connection con=null; //数据库连接
PreparedStatement past=null;//执行sql语句
ResultSet rs=null; //结果集
List list=new ArrayList(); //list 添加信息
Gamegame=null; //声明Game类
/**
* 实现增加方法
* @author Administrator
*
*/
public int GameInsert(Game game) {
//sql 语句
String sql="insert into game values(?,?,?,?)";
//添加信息
String [] getValues={game.getGame_name(),game.getGame_type(),game.getGame_company(),game.getGame_year()+""};
//调用数据库连接方法
return GameBase.executeUpdate(sql, getValues);
}
/**
* 查询所有游戏信息
*/
public List GameCount() {
String sql="select * from game";
//连接数据库、
con=GameBase.GetConnection();
//传入sql 语句
try {
past=con.prepareStatement(sql);
rs=past.executeQuery();
//读取信息
while(rs.next())
{
int id=rs.getInt("game_id");
//游戏名称
String name=rs.getString("game_name");
//游戏类型
String type=rs.getString("game_type");
//游戏发行公司
String company=rs.getString("game_company");
//游戏发行时间
int year=rs.getInt("game_year");
//添加到构造函数里
Game game=new Game(id,name,type,company,year);
//添加到list集合
list.add(game);
}
} catch (SQLException e) {
System.out.println("GameCount 方法出现异常");
e.printStackTrace();
}finally
{
GameBase.colseAll(con, past, rs);
}
return list;
}
/**
* 多条件查询
*/
public List SelectGame(Game game) {
List list=new ArrayList(); //list 添加信息
//sql 语句
String sql="select * from game where 1=1";
//连接数据库
con=GameBase.GetConnection();
try {
//根据名称查询时
if(game.getGame_name()!=""){
sql=sql+"and game_name='"+game.getGame_name()+"'";
}
//根据类型查询时
if (game.getGame_type()!="") {
sql=sql+"and game_type='"+game.getGame_type()+"'";
}
//根据公司名称查询时
if (game.getGame_company()!="") {
sql=sql+"and game_company='"+game.getGame_company()+"'";
}
//根据年份查询时
if (game.getGame_year()!=0) {
sql=sql+"and game_year="+game.getGame_year();
}
//传入sql
past=con.prepareStatement(sql);
//执行sql
rs=past.executeQuery();
//读取信息
while(rs.next()){
int id=rs.getInt("game_id");
//游戏名称
String name=rs.getString("game_name");
//游戏类型
String type=rs.getString("game_type");
//游戏发行公司
String company=rs.getString("game_company");
//游戏发行时间
int year=rs.getInt("game_year");
//添加到构造函数里
Game games=new Game(id,name,type,company,year);
//添加到list集合
list.add(games);
}
} catch (SQLException e) {
System.out.println("SelectGame 方法出现异常");
e.printStackTrace();
}finally{
GameBase.colseAll(con, past, rs);
}
return list;
}
}
package s2.jsp.zhangxiao.entity;
/**
* 游戏类
* @author Administrator
*
*/
public class Game {
private int game_id;
private String game_name;
private String game_type;
private String game_company;
private int game_year;
/**
* 有参构造函数
* @param game_company
* @param game_id
* @param game_name
* @param game_type
* @param game_year
*/
public Game( int game_id, String game_name,
String game_type,String game_company, int game_year) {
super();
this.game_company = game_company;
this.game_id = game_id;
this.game_name = game_name;
this.game_type = game_type;
this.game_year = game_year;
}
/**
* 无惨构造函数
*/
public Game()
{
}
/**
* 游戏编号
* @return
*/
public int getGame_id() {
return game_id;
}
/**
* 游戏编号
* @return
*/
public void setGame_id(int game_id) {
this.game_id = game_id;
}
/**
* 游戏名称
* @return
*/
public String getGame_name() {
return game_name;
}
/**
* 游戏名称
* @return
*/
public void setGame_name(String game_name) {
this.game_name = game_name;
}
/**
* 游戏类型
* @return
*/
public String getGame_type() {
return game_type;
}
/**
* 游戏类型
* @return
*/
public void setGame_type(String game_type) {
this.game_type = game_type;
}
/**
* 发行公司
* @return
*/
public String getGame_company() {
return game_company;
}
/**
* 发行公司
* @return
*/
public void setGame_company(String game_company) {
this.game_company = game_company;
}
/**
* 发行年份
* @return
*/
public int getGame_year() {
return game_year;
}
/**
* 发行年份
* @return
*/
public void setGame_year(int game_year) {
this.game_year = game_year;
}
}
看了看学习学习哈哈哈哈 学习学习学习学习
学习一下。谢谢:victory: 这个项目太棒勒!下下来学习下! 感谢 分享 不错不错不错,学习一下 殇星 发表于 2016-1-6 18:12
学习学习学习学习
学习学习学习学习
页:
[1]
2