老师帮看下我的代码,好像有个bug,当输入超过指定长度时会显示插入正常,但是数据库没有信心

老师帮看下我的代码,好像有个bug,当输入超过指定长度时会显示插入正常,但是数据库没有信心

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<title>添加数据</title>

<link rel="stylesheet" type="text/css" href="css/addcourse.css">

<script type="text/javascript" src="js/jQuery.js"></script>

<script type="text/javascript" src="js/addcourse.js"></script>

</head>

<body>

<div class="div1">

<div class="div2">

<p class="p1">${requestScope.error}</p>

    <p class="p2">课程添加</p>

<form action="${pageContext.request.contextPath}/addsourse" method="post">

<ul>

<li><span>课程名称:</span><input type="text" name="name"></li>

<li><span>所属方向:</span><input type="text" name="category"></li>

<li><span>课程描述:</span><input type="text" name="desc"></li>

<li><input type="submit"  name="submit" value="添加"></li>

<li><input type="button" name="search" onclick="window.location.href='${pageContext.request.contextPath}/search'" value="查询所有课程信息"></li>

</ul>

</form>

</div>

</div>


</body>

</html>


package com.liujia.connectionjdbcutils;


import java.sql.Connection;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;


import com.mchange.v2.c3p0.ComboPooledDataSource;


public class ConnectionUtil {

private static final ComboPooledDataSource cpl=new ComboPooledDataSource();

public static Connection getConnection() throws ClassNotFoundException, SQLException {

Connection cnn=cpl.getConnection();

return cnn;

}

public static void releas(Connection cnn,Statement pst) {

if(cnn!=null) {

try {

cnn.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

if(pst!=null) {

try {

pst.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

public static void releas(Connection cnn,Statement pst,ResultSet rs) {

if(cnn!=null) {

try {

cnn.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

if(pst!=null) {

try {

pst.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

if(rs!=null) {

try {

rs.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}


}


package com.liujia.course;


public class Course {

private int cid;

private String cname;

private String ccategory;

private String cdesc;

private String ctime;

public Course() {

}

public Course(int cid, String cname, String ccategory, String cdesc, String ctime) {

super();

this.cid = cid;

this.cname = cname;

this.ccategory = ccategory;

this.cdesc = cdesc;

this.ctime = ctime;

}

public int getCid() {

return cid;

}

public void setCid(int cid) {

this.cid = cid;

}

public String getCname() {

return cname;

}

public void setCname(String cname) {

this.cname = cname;

}

public String getCcategory() {

return ccategory;

}

public void setCcategory(String ccategory) {

this.ccategory = ccategory;

}

public String getCdesc() {

return cdesc;

}

public void setCdesc(String cdesc) {

this.cdesc = cdesc;

}

public String getCtime() {

return ctime;

}

public void setCtime(String ctime) {

this.ctime = ctime;

}

@Override

public String toString() {

return "[cid=" + cid + ", cname=" + cname + ", ccategory=" + ccategory + ", cdesc=" + cdesc + ", ctime="

+ ctime + "]";

}


}


package com.liujia.service;


import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.util.ArrayList;

import java.util.List;



import com.liujia.connectionjdbcutils.ConnectionUtil;

import com.liujia.course.Course;


public class Service {


public static boolean Insert(String name,String category,String desc) {


boolean flag=true;

Connection cnn=null;

PreparedStatement pst=null;

try {

cnn=ConnectionUtil.getConnection();

String sql="INSERT INTO course VALUES(null,?,?,?,now())";

pst=cnn.prepareStatement(sql);

pst.setString(1, name);

pst.setString(2, category);

pst.setString(3, desc);

int num=pst.executeUpdate();

System.out.println(num);

if(num>0) {

System.out.println("能正常插入数据库");

flag=true;

}else {

System.out.println("无法插入数据库");

flag=false;

}

}catch(Exception e) {

e.printStackTrace();

}finally {

ConnectionUtil.releas(cnn, pst);

}

return flag;

}

public static List getNews() {

List clist=new ArrayList();

Course course=new Course();

Connection cnn=null;

PreparedStatement pst=null;

ResultSet rs=null;

try {

cnn=ConnectionUtil.getConnection();

String sql="SELECT*FROM course";

pst=cnn.prepareStatement(sql);

rs=pst.executeQuery();

while(rs.next()) {

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

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

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

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

String ctime=rs.getString("createTime");

clist.add(new Course(id,name,category,desp, ctime));

}

}catch(Exception e) {

e.printStackTrace();

}finally {

ConnectionUtil.releas(cnn, pst,rs);

}

return clist;

}

}


package com.liujia.servlet;


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.liujia.service.Service;


/**

 * Servlet implementation class addServlet

 */

@WebServlet("/addsourse")

public class addServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

       

    /**

     * @see HttpServlet#HttpServlet()

     */

    public addServlet() {

        super();

        // TODO Auto-generated constructor stub

    }


/**

* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

*/

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

request.setCharacterEncoding("UTF-8");

    response.setContentType("text/html;charset=UTF-8");

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

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

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


boolean f=Service.Insert(name, category, desc);

if(f) {

System.out.println("插入数据成功");

request.setAttribute("error","数据插入成功!");

request.getRequestDispatcher("/search").forward(request, response);

}else {

System.out.println("插入数据失败");

request.getSession().setAttribute("error","插入失败略略略!");

// response.sendRedirect("/addcourse.jsp");

request.getRequestDispatcher("addcourse.jsp").forward(request, response);

}

}


/**

* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

*/

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

doGet(request, response);

}


}


package com.liujia.servlet;


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.liujia.service.Service;


/**

 * Servlet implementation class searchServlet

 */

@WebServlet("/search")

public class searchServlet extends HttpServlet {

private static final long serialVersionUID = 1L;


/**

* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

*/

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

List cl=Service.getNews();

request.getServletContext().setAttribute("cl", cl);

request.getRequestDispatcher("/showcourse.jsp").forward(request, response);

// response.sendRedirect("/JdbcWebProject/showcourse.jsp");


}


/**

* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

*/

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

doGet(request, response);

}


}




.div1{

width:50%;

margin: 0 auto;

text-align: center;

}


.div2{

width:400px;

height: auto;

margin: 0 auto;

text-align: center;

background-color: #DCDCDC;

position: relative;

}


.div2 p{

padding-top: 10px;

}


.div2 .p1{

color: red;

font-size: 30px;

}


.div2 .p2{

font-size: 20px;

font-weight: bold;

}


.div2 li{

margin-top: 10px;

padding-bottom: 10px;

list-style: none;


}


.div2 span{

margin-right: 10px;

}


div{

margin: 0 auto;

text-align: center;

}


.div1 .div2 p{

color: red;

font-size: 30px;

margin-top: 10%;

top: 15px;

position: relative;

}


.div1 .div2{

width: 30%;

height: auto;

background-color: #C0C0C0;

}



.div1 .div2 table{

width: 100%;

background-color: #F5F5F5;


}


.div1 .div2 table tr{

border: 1px solid black;


}


.div1 input{

margin-top:20px;

height:30px;

}


<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<!DOCTYPE html>

<html>

<head>

<title>展示数据</title>

<link rel="stylesheet" type="text/css" href="css/show.css">

</head>

<body>

<div class="div1">

<div class="div2">

<p>${requestScope.error}</p>

<table>

<tr>

<td>课程ID</td>

<td>课程名称</td>

<td>所属方向</td>

<td>课程描述</td>

<td>创建时间</td>

</tr>

<c:forEach  items="${applicationScope.cl}" var="c"  varStatus="idx">

<tr>

<td>${c.cid}</td>

<td>${c.cname}</td>

<td>${c.ccategory}</td>

<td>${c.cdesc}</td>

<td>${c.ctime}</td>

</tr>

</c:forEach>

</table>

</div>

<input type="button" name="search" onclick="window.location.href='${pageContext.request.contextPath}/addcourse.jsp'" value="返回继续添加">

</div>

</body>

</html>


正在回答

登陆购买课程后可参与讨论,去登陆

1回答

同学你好,测试代码,当输入超过指定长度时,程序是会报错的,比如:

报错位置:

http://img1.sycdn.imooc.com//climg/5f476096091f3f8e07910362.jpg

报错信息:

http://img1.sycdn.imooc.com//climg/5f475f0d09262d6211920141.jpg

这并不是一个bug,而是在当传入的数据超过字段长度限制,数据库就会报错。

在实际开发中,这里的数据长度一般在项目设计的时候,针对字段内容就要考虑到,然后在前端页面或者后台接收数据时就可以加上验证内容。

如果我的回答解决了你的疑惑,请采纳!祝学习愉快!

  • 大圣归来² 提问者 #1
    后台接收验证可以理解为在插入数据库之前就校验吗?
    2020-08-27 15:36:33
  • 好帮手慕小班 回复 提问者 大圣归来² #2
    同学你好,可以这样理解。 继续加油 祝:学习愉快~
    2020-08-27 17:35:04
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师