Java web亂碼處理JDBC游戲管理系統(tǒng)實現(xiàn)查詢?nèi)浚琂SP,servlet,學(xué)習(xí)筆記【詩書畫唱】






1、創(chuàng)建表:每個表應(yīng)該有一個與業(yè)務(wù)無關(guān)的主鍵(int類型)
2、項目的準(zhǔn)備工作:導(dǎo)入mysql數(shù)據(jù)庫的驅(qū)動包和db.properties文件(src目錄下)
"3、搭建一下項目的框架:項目中的代碼不是寫在一個類里面運行就可以了,我們應(yīng)該
把代碼有條理的把它寫在不同的java類中,讓他們一起工作。"
簡單分層處理:
com.jy.bean:存放表對應(yīng)的數(shù)據(jù)模型的。每個表都對應(yīng)一個bean類
com.jy.dao:用來對表進(jìn)行增刪改查的類。每個表都對應(yīng)個dao類
com.jy.controller:就是存放servlet的。
com.jy.utils:存放項目中的一些常用工具類的,創(chuàng)建數(shù)據(jù)庫的連接工具類
4、寫界面:創(chuàng)建游戲管理查詢界面,游戲列表界面和游戲新增界面
5、實現(xiàn)按鈕的跳轉(zhuǎn)頁面的功能,具體的查詢新增功能暫時不需要實現(xiàn)

--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)
--'詩書畫唱游戲發(fā)行年份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類,用來存放對應(yīng)表中的數(shù)據(jù)的
//這個類的名字跟對應(yīng)的表的名字是一樣的,但是首字母必須大寫
//這個類中的屬性名與表中的列名是一致的
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、查詢數(shù)據(jù)庫:
GameDao userDao = new GameDao();
// String msg = "";
//
//
//
// int num=1;
?
// String name2 =null;
//
// if(name!= null) {
//
// //自己的總結(jié):
//
// //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;
//類名就是對應(yīng)的bean的名字后面加上Dao
//Dao就是數(shù)據(jù)訪問類,就是對Game表進(jìn)行增刪改查的代碼都寫在這個類中
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;
? ? //靜態(tài)塊,隨著類加載而運行的
? ? static{
? ? //讀取db.properties文件中的內(nèi)容
? ? 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>發(fā)行公司:</td>
? ? ? ?<td><input class="inp" type="text" name="gcomp" /></td>
? ? ? ? ? ? ? ? ? ? ? ? </tr>
? ? ? ? ? ? ? ? ? ? ? ? <tr>
? ? ? ? ? ? ? ? ? ? ? ? ? ? <td>發(fā)行年份:</td>
? ? ? ?<td><input class="inp" type="text" name="gyear"
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? placeholder="四位數(shù)字" /></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>發(fā)行公司:</td>
?<td><input class="inp" type="text" name="gcomp"/></td>
? ? ? ? ? ? ? ? ? ? ? ? </tr>
? ? ? ? ? ? ? ? ? ? ? ? <tr>
? ? ? ? ? ? ? ? ? ? ? ? ? ? <td>發(fā)行年份:</td>
?<td><input class="inp" type="text" name="gyear"
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? placeholder="四位數(shù)字" /></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>發(fā)行公司</th>
? ? ? ? ? ? ? ? ? ? ? ? ? ? <th>發(fā)行時間</th>
? ? ? ? ? ? ? ? ? ? ? ? </tr>
? ? ? ? ? ? ? ? ? ? ? ? <%=html%>
? ? ? ? ? ? ? ? ? ? </table>
? ? ? ? ? ? ? ? </td>
? ? ? ? ? ? </tr>
? ? ? ? </table>
? ? </body>
</html>




