Java web乱码处理JDBC游戏管理系统实现查询全部,JSP,servlet,学习笔记【诗书画唱】






1、创建表:每个表应该有一个与业务无关的主键(int类型)
2、项目的准备工作:导入mysql数据库的驱动包和db.properties文件(src目录下)
"3、搭建一下项目的框架:项目中的代码不是写在一个类里面运行就可以了,我们应该
把代码有条理的把它写在不同的java类中,让他们一起工作。"
简单分层处理:
com.jy.bean:存放表对应的数据模型的。每个表都对应一个bean类
com.jy.dao:用来对表进行增删改查的类。每个表都对应个dao类
com.jy.controller:就是存放servlet的。
com.jy.utils:存放项目中的一些常用工具类的,创建数据库的连接工具类
4、写界面:创建游戏管理查询界面,游戏列表界面和游戏新增界面
5、实现按钮的跳转页面的功能,具体的查询新增功能暂时不需要实现

--drop table game
--select * from game
create table game(
id int primary key auto_increment,
gname varchar(100) ,
gtype varchar(100) ,
gcompany varchar(100) ,
gyear int);
--gyear varchar(100)
--'诗书画唱游戏发行年份1'
insert into game(
gname,
gtype ,
gcompany ,
gyear
)
values ("SSHC1","type1",'SSHCcompany1',666),
("SSHC2","type2",'SSHCcompany2',888);
--values ("诗书画唱游戏1","诗书画唱游戏类型1",'诗书画唱公司1',666),
--("诗书画唱游戏2","诗书画唱游戏类型2",'诗书画唱公司2',888);




package com.SSHC.bean;
//这个类叫bean类,用来存放对应表中的数据的
//这个类的名字跟对应的表的名字是一样的,但是首字母必须大写
//这个类中的属性名与表中的列名是一致的
public class Game {
private Integer id;//游戏编号
private String gname;//游戏名称
private String gtype;//游戏类型
private String gcompany;//游戏公司
private Integer gyear;//游戏年份
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getGname() {
return gname;
}
public void setGname(String gname) {
this.gname = gname;
}
public String getGtype() {
return gtype;
}
public void setGtype(String gtype) {
this.gtype = gtype;
}
public String getGcompany() {
return gcompany;
}
public void setGcompany(String gcompany) {
this.gcompany = gcompany;
}
public Integer getGyear() {
return gyear;
}
public void setGyear(Integer gyear) {
this.gyear = gyear;
}
}



package com.SSHC.controller;
import java.io.IOException;
import java.util.List;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.SSHC.DAO.GameDao;
import com.SSHC.bean.Game;
/**
* Servlet implementation class gameServlet
*/
@WebServlet("/gameServlet")
public class gameServlet extends HttpServlet implements Servlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public gameServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
this.doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
//中文乱码处理:
// request.setCharacterEncoding("utf-8");
//1、获取用户输入的账号和密码:
//
// String act = request.getParameter("act");
//
// String pwd = request.getParameter("pwd");
//
//2、查询数据库:
GameDao userDao = new GameDao();
// String msg = "";
//
//
//
// int num=1;
// String name2 =null;
//
// if(name!= null) {
//
// //自己的总结:
//
// //new String(name.getBytes("iso8859-1【大多情况不变,因为这种编码格式很多情况中都通用】"),"(被接收到的这个文件界面的UpdateTable.jsp等文件的格式)") ;
//
// name2= new String(name.getBytes("iso8859-1"),"UTF-8") ;
//
//
//
// }
// System.out.println(name2);
//
List<Game> list = userDao.selectAll();
for(Game U : list) {
Integer Id= U.getId();
String Gtype=U.getGtype();
System.out.println(Id);
System.out.println(new String(Gtype.getBytes("iso8859-1"),"UTF-8"));
// if(act.equals(act1)) {
//
// num=num*0;
//
// } else {
//
// num=num*2;
//
// }
// } //有时用System.out.println(html)等来测试,找BUG等;
//
//
// if(num==0) {
//
// msg = "该账号已被使用";
//
// request.setAttribute("msg", msg);
//
// request.getRequestDispatcher("dupName.jsp")
//
// .forward(request, response);
//
// } else {
//
// msg = "该账号可以注册";
//
// request.setAttribute("msg", msg);
//
// request.getRequestDispatcher("success.jsp")
//
// .forward(request, response);
//
}
}
}


package com.SSHC.DAO;
//类名就是对应的bean的名字后面加上Dao
//Dao就是数据访问类,就是对Game表进行增删改查的代码都写在这个类中
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 com.SSHC.Utils.DBUtils;
import com.SSHC.bean.Game;
public class GameDao {
public List<Game>selectAll(){
//有时String sql = "select * from game";写上面会报空指针异常
Connection conn = null;
PreparedStatement pstm = null;
ResultSet rs = null;
List<Game>list = new ArrayList<Game>();
try {
conn = DBUtils.getConn();
String sql = "select * from game where 1=1";
pstm = conn.prepareStatement(sql);
rs = pstm.executeQuery();
while(rs.next()) {
Integer id = rs.getInt("id");
String Gname = rs.getString("Gname");
String Gcompany = rs.getString("Gcompany");
String Gtype = rs.getString("Gtype");
Integer Gyear= rs.getInt("Gyear");
Game Game = new Game();
Game.setId(id);
Game.setGname(Gname);
Game.setGcompany(Gcompany);
Game.setGtype(Gtype);
Game.setGyear(Gyear);
list.add(Game);
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
}


package com.SSHC.Utils;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;
public class DBUtils {
private static String driverName;
private static String url;
private static String userName;
private static String pwd;
//静态块,随着类加载而运行的
static{
//读取db.properties文件中的内容
Properties prop = new Properties();
InputStream is = DBUtils.class.getClassLoader()
.getResourceAsStream("db.properties");
try {
prop.load(is);
driverName = prop.getProperty("dn");
url = prop.getProperty("url");
userName = prop.getProperty("un");
pwd = prop.getProperty("up");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection getConn(){
Connection conn = null;
try {
Class.forName(driverName);
conn = DriverManager.getConnection(url,userName,pwd);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public void close(ResultSet rs,PreparedStatement pstm
,Connection conn){
try {
if(rs != null) {
rs.close();
}
if(pstm != null) {
pstm.close();
}
if(conn != null) {
conn.close();
}
} catch(Exception e) {
e.printStackTrace();
}
}
}


dn=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/firstjsp?useUnicode=true&characterEncoding=UTF-8
un=root
up=root


<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base hreff="<%=basePath%>">
<title></title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<style type="text/css">
* {
font-size:23px;
}
.inp {
box-shadow: 5px 5px 5px #888888;
border: 0px;
border-radius: 5px;
}
</style>
<script type="text/javascript">
function fanHui(){
window.location.href = 'gameinfo.jsp';
}
</script>
</head>
<body>
<table style="width:100%;">
<tr>
<td style="background-color:gray;text-align:center;">游戏新增</td>
</tr>
<tr>
<td align="center">
<table>
<tr>
<td>游戏名称:</td>
<td>
<input class="inp" type="text" name="gname" />
<span style="color:red;">*</span>
</td>
</tr>
<tr>
<td>游戏类型:</td>
<td><input class="inp" type="text" name="gtype" /></td>
</tr>
<tr>
<td>发行公司:</td>
<td><input class="inp" type="text" name="gcomp" /></td>
</tr>
<tr>
<td>发行年份:</td>
<td><input class="inp" type="text" name="gyear"
placeholder="四位数字" /></td>
</tr>
<tr>
<td colspan="2" align="center">
<%--
下面记得onclick="fanHui();"中的分号别少了。
--%>
<input type="submit" value="添加" />
<input type="button" value="返回" onclick="fanHui();"/>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>


<%@ page language="java" contentType="text/html;
charset=UTF-8" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()
+"://"+request.getServerName()
+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base hreff="<%=basePath%>">
<title></title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords"
content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<style type="text/css">
* {
font-size: 23px;
}
.inp {
box-shadow: 5px 5px 5px #888888;
border: 0px;
border-radius: 5px;
}
</style>
<script type="text/javascript">
function toAdd(){
window.location.href = 'add.jsp';
}
</script>
</head>
<body>
<table style="width:100%;">
<tr>
<td style="background-color:gray;text-align:center;">
游戏查询</td>
</tr>
<tr>
<td align="center">
<form action="gamelist.jsp" method="post">
<table>
<tr>
<td>游戏名称:</td>
<td><input class="inp" type="text" name="gname" /></td>
</tr>
<tr>
<td>游戏类型:</td>
<td><input class="inp" type="text" name="gtype" /></td>
</tr>
<tr>
<td>发行公司:</td>
<td><input class="inp" type="text" name="gcomp"/></td>
</tr>
<tr>
<td>发行年份:</td>
<td><input class="inp" type="text" name="gyear"
placeholder="四位数字" /></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="查询" />
<input type="button" value="新增" onclick="toAdd();" />
</td>
</tr>
</table>
</form>
</td>
</tr>
</table>
</body>
</html>


<%@ page language="java" contentType="text/html;
charset=UTF-8" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()
+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@page import="com.SSHC.bean.Game"%>
<%@page import="com.SSHC.DAO.GameDao"%>
<%@page import="java.util.List"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base hreff="<%=basePath%>">
<title></title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<style type="text/css">
* {
font-size: 23px;
}
</style>
</head>
<body>
<%
GameDao userDao = new GameDao();
List<Game> list = userDao.selectAll();
StringBuilder html = new StringBuilder();
for(Game U : list) {
html.append("<tr><td>"
+new String(U.getGname().getBytes("iso8859-1"),"UTF-8")
+ "</td><td>"+new String(U.getGtype().getBytes("ISO-8859-1"),"UTF-8")
+"</td><td>" + new String(U.getGcompany().
getBytes("ISO-8859-1"),"UTF-8")
+"</td><td>" + U.getGyear()+"</td></tr>");
/*** Integer Id= U.getId();
String Gname= U.getGname();
String Gtype= U.getGtype();
String Gcompany= U.getGcompany();
System.out.println(Id);
*/
}
%>
<table style="width:100%;" border="1">
<tr>
<td><a hreff="gameinfo.jsp" style="float:right;">返回</a></td>
</tr>
<tr>
<td style="background-color:gray;text-align:center;">
游戏列表</td>
</tr>
<tr>
<td>
<table border="1" style="width:100%;">
<tr>
<th>游戏名称</th>
<th>游戏类别</th>
<th>发行公司</th>
<th>发行时间</th>
</tr>
<%=html%>
</table>
</td>
</tr>
</table>
</body>
</html>




