欢迎光临散文网 会员登陆 & 注册

servlet运行不了,正则表达式增删改查组合查询,中文乱码JDBC游戏管理系统JSP诗书画唱

2020-09-23 22:07 作者:诗书画唱  | 我要投稿



--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 ("诗书画唱游戏1","诗书画唱游戏类型1",'诗书画唱好公司1',6666),

("诗书画唱游戏2","诗书画唱好游戏类型2",'诗书画唱好公司2',8888),

("诗书画唱好游戏3","诗书画唱好游戏类型3",'诗书画唱公司3',2333),

("诗书画唱好游戏4","诗书画唱游戏类型4",'诗书画唱公司4',7758);


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 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 addOKServlet

 */

@WebServlet("/addOKServlet")

public class addOKServlet extends HttpServlet {

private static final long serialVersionUID = 1L;


    /**

     * @see HttpServlet#HttpServlet()

     */

    public addOKServlet() {

        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  gname = request.getParameter("gname");

    String gtype= request.getParameter("gtype");

    String gcompany= request.getParameter("gcompany");

String gyear = request.getParameter("gyear");

  // Integer gyear2=Integer.valueOf(gyear);

Integer gyearInteger=0;


if(gyear != null && gyear.length() > 0) {


gyearInteger =Integer.valueOf(gyear);


}


    Game u = new Game();

    u.setGname(gname);

    u.setGtype(gtype);

 

    u.setGcompany(gcompany);

   u.setGyear(gyearInteger);

   

      

GameDao ud = new GameDao();


  ud.add(u);

   

//Integer count = ud.add(u);


String S=null;

    if(u.getGname()!=null) {

  S="添加成功!";

    }else 

    {

    S="添加失败!";

    }  


  //将html代码传到gamelist.jsp页面去

  request.setAttribute("S", S);

  //跳转到gamelist.jsp页面

  request.getRequestDispatcher("addOK.jsp")

      .forward(request, response);

}


}



package com.SSHC.controller;


import java.io.IOException;

import java.util.List;


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 GameListServ

 */

@WebServlet("/gameList")

public class GameListServ extends HttpServlet {

private static final long serialVersionUID = 1L;

       

    /**

     * @see HttpServlet#HttpServlet()

     */

    public GameListServ() {

        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");

//获取到用户输入的查询条件


String gname = request.getParameter("gname");

String gtype = request.getParameter("gtype");

String gcomp = request.getParameter("gcompany");

String strYear = request.getParameter("gyear");

//将字符串strYear转换成Integer

Integer gyear = -1;

if(strYear != null && strYear.length() > 0) {

gyear = Integer.parseInt(strYear);

}

//将查询条件打包

Game gm = new Game();

gm.setGname(gname);

gm.setGtype(gtype);

gm.setGcompany(gcomp);

gm.setGyear(gyear);

//根据条件进行查询

GameDao gd = new GameDao();

List<Game>list = gd.selectByCond(gm);

//拼接html字符串

StringBuilder html = new StringBuilder();

for(Game g : list) {

String gn = g.getGname();

String gt = g.getGtype();

String gc = g.getGcompany();

Integer gy = g.getGyear();

html.append("<tr>");

        html.append("<td>" + gn + "</td>");

        html.append("<td>" + gt + "</td>");

        html.append("<td>" + gc + "</td>");

        html.append("<td>" + gy + "</td>");

        html.append("</tr>");

}

//将html代码传到gamelist.jsp页面去

request.setAttribute("html", html);

//跳转到gamelist.jsp页面

request.getRequestDispatcher("gamelist.jsp")

    .forward(request, response);

}


}

package com.SSHC.DAO;


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.bean.Game;

import com.SSHC.utils.DBUtils;


//类名就是对应的bean的名字后面加上Dao

//Dao就是数据访问类的意思,就是对Game表进行增删改查的代码都写在这个类中

public class GameDao {

    public List<Game>selectAll(){

    Connection conn = null;

    PreparedStatement pstm = null;

    ResultSet rs = null;

    String sql = "select * from game";

    //存放查询结果的容器

    List<Game>list = new ArrayList<Game>();

    try {

        conn = DBUtils.getConn();

pstm = conn.prepareStatement(sql);

rs = pstm.executeQuery();

while(rs.next()) {

Integer id = rs.getInt("id");

String gname = rs.getString("gname");

String gtype = rs.getString("gtype");

String gcomp = rs.getString("gcompany");

Integer gyear = rs.getInt("gyear");

//数据打包

Game g = new Game();

g.setId(id);

g.setGname(gname);

g.setGtype(gtype);

g.setGcompany(gcomp);

g.setGyear(gyear);

list.add(g);

}

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

//清理资源

DBUtils.close(rs, pstm, conn);

}

    return list;

    }

    

    //组合查询

    public List<Game>selectByCond(Game g){

    //定义一个存放参数的容器

    List<Object>params = new ArrayList<Object>();

    //根据g中的条件来拼接出sql语句

    StringBuilder sql = new StringBuilder();

    sql.append("select * from game where 1=1 ");

    String gn = g.getGname();

    if(gn != null && gn.trim().length() > 0) {

    sql.append("and gname like ? ");

    params.add("%" + gn + "%");

    }

    String gt = g.getGtype();

    if(gt != null && gt.trim().length() > 0) {

    sql.append("and gtype like ? ");

    params.add("%" + gt + "%");

    }

    String gc = g.getGcompany();

    if(gc != null && gc.trim().length() > 0) {

    sql.append("and gcompany like ? ");

    params.add("%" + gc + "%");

    }

    Integer gy = g.getGyear();

    if(gy != null && gy > 0) {

    sql.append("and gyear = ? ");

    params.add(gy);

    }

    Connection conn = null;

    PreparedStatement pstm = null;

    ResultSet rs = null;

    //存放查询结果的容器

    List<Game>list = new ArrayList<Game>();

    try {

        conn = DBUtils.getConn();

pstm = conn.prepareStatement(sql.toString());

//设置参数

for(int i = 0;i < params.size();i ++) {

pstm.setObject(i + 1, params.get(i));

}

rs = pstm.executeQuery();

while(rs.next()) {

Integer id = rs.getInt("id");

String gname = rs.getString("gname");

String gtype = rs.getString("gtype");

String gcomp = rs.getString("gcompany");

Integer gyear = rs.getInt("gyear");

//数据打包

Game gm = new Game();

gm.setId(id);

gm.setGname(gname);

gm.setGtype(gtype);

gm.setGcompany(gcomp);

gm.setGyear(gyear);

list.add(gm);

}

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

//清理资源

DBUtils.close(rs, pstm, conn);

}

    return list;

    }

    

 public Integer add(Game  u) {

  

    String sql = "insert into game(gname ,gtype "

+ ",gcompany ,gyear) values (?,?,?,?);";

    Connection conn = null;

    PreparedStatement pstm = null;

    ResultSet rs = null;

//    gname,

//

//    gtype ,

//    gcompany ,

//

//    gyear

    try {

        conn = DBUtils.getConn();

pstm = conn.prepareStatement(sql);

//设置占位符

//System.out.println(u.getAct());

pstm.setObject(1,u.getGname());

pstm.setObject(2,u.getGtype() );

pstm.setObject(3,u.getGcompany ());

pstm.setObject(4, u.getGyear());



pstm.executeUpdate();

        } catch (SQLException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }finally{

 

           

DBUtils.close(rs, pstm, conn);

}

    return 0;

    }

}



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 static 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: 50px;

            }

            

            .inp {

                box-shadow: 5px 5px 5px #888888;

                border: 0px;

                border-radius: 5px;

            }

        </style>

        <script type="text/javascript">

            function cancel(){

            window.location.href = 'gameinfo.jsp';

            }

          

         

            //表单验证的方法

            function doCheck(){

            //获取输入的年份

            var year = document.getElementsByName('gyear')[0].value;

            //判断year是一个四位的数字

            //\d表示0到9的数字,{4}表示可以有4个这样的数字

            //^以数字开头,$表示以数字结尾

            var reg = /^\d{4}$/;

            //如果year是一个四位数字,那么r就为true,否则就为false

            var r = reg.test(year);

            if(!r) {

            alert('请输入四位数字');

            }

            return r;

            }

    

        </script>

    </head>

    <body>

        <table style="width:100%;">

            <tr>

 <td style="background-color:gray;text-align:center;">游戏新增</td>

            </tr>

            <tr>

                <td align="center">

                   <%--<form action="addOK.jsp" method="post"> --%> 

                   <form action="addOKServlet" method="post" onsubmit="return doCheck();">

                    <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="gcompany" /></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="添加" onclick='doCheck();' />

 <input type="button" value="返回" onclick="cancel();" />

                            </td>

                        </tr>

                    </table>

                    </form>

                </td>

            </tr>

        </table>

    </body>

</html>


<%@ page language="java" contentType=

"text/html; charset=utf-8"

    pageEncoding="utf-8"%>

    <%@page import="com.SSHC.bean.Game"%>

      <%@page import="com.SSHC.DAO.GameDao"%>

    <%--

     <%

    //乱码处理:

    request.setCharacterEncoding("utf-8");

    //1、获取表单中输入的数据:

    String  gname = request.getParameter("gname");

    String gtype= request.getParameter("gtype");

    String gcompany= request.getParameter("gcompany");

 String gyear = request.getParameter("gyear");

  // Integer gyear2=Integer.valueOf(gyear);

  Integer gyearInteger=0;


if(gyear != null && gyear.length() > 0) {


gyearInteger =Integer.valueOf(gyear);


}


    Game u = new Game();

    u.setGname(gname);

    u.setGtype(gtype);

 

    u.setGcompany(gcompany);

   u.setGyear(gyearInteger);

   

      

GameDao ud = new GameDao();


  ud.add(u);

   

 //Integer count = ud.add(u);


 String S=null;

    if(u.getGname()!=null) {

  S="添加成功!";

    }else 

    {

    S="添加失败!";

    }  

%>

--%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01

 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Insert title here</title>

</head>

<body>

${S };

</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: 50px;

            }

            

            .inp {

                box-shadow: 5px 5px 5px #888888;

                border: 0px;

                border-radius: 5px;

            }

        </style>

        <script type="text/javascript">

            function toAdd(){

            window.location.href = 'add.jsp';

            }

            

            //表单验证的方法

            function doCheck(){

            //获取输入的年份

            var year = document.getElementsByName('gyear')[0].value;

            //判断year是一个四位的数字

            //\d表示0到9的数字,{4}表示可以有4个这样的数字

            //^以数字开头,$表示以数字结尾

            var reg = /^\d{4}$/;

            //如果year是一个四位数字,那么r就为true,否则就为false

            var r = reg.test(year);

            if(!r) {

            alert('请输入四位数字');

            }

            return r;

            }

        </script>

    </head>

    <body>

        <table style="width:100%;">

            <tr>

                <td style="background-color:gray;text-align:center;">游戏查询</td>

            </tr>

            <tr>

                <td align="center">

 <!-- action中的路径都配置成servlet,通过servlet来进行页面的跳转 -->

<form action="gameList" method="post" onsubmit="return doCheck();">

                    <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+"/";

%>

<!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: 50px;

            }

        </style>

        <script type="text/javascript">

            function bk(){

            window.location.href = 'gameinfo.jsp';

            }

        </script>

    </head>

    <body>

        <table style="width:100%;" border="1">

            <tr>

                <td><a hreff="javascript:bk()" 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>




servlet运行不了,正则表达式增删改查组合查询,中文乱码JDBC游戏管理系统JSP诗书画唱的评论 (共 条)

分享到微博请遵守国家法律