java web的jsp里text获取不到数据,只获取到null!求解,相关代码如下!

我屏蔽掉那里则能存到数据库,但是是null,已经填了东西上去了,如果不屏蔽的话输出“书名不能为空”,没有报错!System.out.println(sql);了一下是输出insert into book(bookname,bookauthor,bookpress,bookedition,booklocation) values('null','null',null,'null','null'),这要怎么解决,求大佬帮忙!

//添加书本(控制部分)
public boolean addBook(String bookname, String bookauthor, String bookpress,String bookedition,String booklocation)
{
    Connection conn = null;
    Statement stmt = null;
    try {
        conn = Database.getConnection();
        stmt = conn.createStatement();
        String sql = "insert into book(bookname,bookauthor,bookpress,bookedition,booklocation) "
                + "values('"+bookname+"','"+bookauthor+"',"+bookpress+",'"+bookedition+"','"+booklocation+"')";
        //System.out.println(sql);
        stmt.execute(sql);
        return true;
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return false;
    } finally {
        Database.close(conn, stmt);
    }
}

//用的是servlet的doget和dopost,下面为servlet部分
            String bookname = request.getParameter("bookname");//初始化图书的各个字段
        String bookauthor = request.getParameter("bookauthor");
        String bookpress = request.getParameter("bookpress");
        String bookedition = request.getParameter("bookedition");
        String booklocation = request.getParameter("booklocation");

// if(bookname==null||bookname.trim().isEmpty()){

// request.setAttribute("msg", "书名不能为空");

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

// return;

// }

// if(bookauthor==null||bookauthor.trim().isEmpty()){

// request.setAttribute("msg", "作者不能为空");

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

// return;

// }

// if(bookpress==null||bookpress.trim().isEmpty()){

// request.setAttribute("msg", "出版社不能为空");

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

// return;

// }
// if(bookedition==null||bookedition.trim().isEmpty()){

// request.setAttribute("msg", "版本不能为空");

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

// return;

// }
// if(booklocation==null||booklocation.trim().isEmpty()){

// request.setAttribute("msg", "出版存放位置不能为空");

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

// return;

// }

  Operator operate = new Operator();
        boolean isSuccess = operate.addBook(bookname, bookauthor, bookpress, bookedition,booklocation);
        if(isSuccess){
            request.setAttribute("msg", "增加成功");
            request.getRequestDispatcher("/addbook.jsp").forward(request, response);  
            return;
        }else{
            request.setAttribute("msg", "增加失败");
            request.getRequestDispatcher("/addbook.jsp").forward(request, response);  
            return;
        }

        //下面是jsp部分
        <form action="AddBookServlet" method="post" enctype="multipart/form-data">
    <div>
    <input type="text" name="bookname" placeholder="请输入书名(如:三国演义)"></div>     
    <div>
    <input type="text" name="bookauthor" placeholder="请输入作者(如:张三)"></div> 
    <div>
    <input type="text" name="bookpress" placeholder="请输入出版社(如:中山大学出版社)"></div>
    <div>
    <input type="text" name="bookedition" placeholder="请输入版本(如:第一版)"></div> 
    <div>
    <input type="text" name="booklocation" placeholder="请输入存放位置(如:1楼101室2019)"></div>     
    <!--  <div>
    <a href="javascript:;" class="a-upload">点击这里上传文件(.txt文件)
    <input type="file" name="file" id="file">
    </a></div>  -->      
    <div>   
    <input type="submit" value="增加图书" id="addbook">
    </div>       
    <div>       
    <a href="welcome.jsp" id="back">返回菜单</a></div></br>
    </form>

    <font color="red" size="2"> ${msg }</font>

1个回答

把enctype="multipart/form-data"去掉跑一遍试下

shuai8624
臭小子帅 回复weixin_41359495: 加qq群问下 700637673
4 个月之前 回复
weixin_41359495
H_Franky 谢谢,可以了,其中一个少了‘’
4 个月之前 回复
weixin_41359495
H_Franky 数字确实是可以了,但是utf8在数据库这边也设置了,在代码这边也设置了request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8");
4 个月之前 回复
weixin_41359495
H_Franky 回复臭小子帅: 数据库表没有存到,接受是可以了,但是这个报错怎么解决?对应列了啊,除了第一列是自增的id
4 个月之前 回复
shuai8624
臭小子帅 回复weixin_41359495: 好吧,那是接收到了啊,中文注意乱码
4 个月之前 回复
weixin_41359495
H_Franky 数据库表第一列是自增的id
4 个月之前 回复
weixin_41359495
H_Franky 数据库没有这条信息
4 个月之前 回复
weixin_41359495
H_Franky 报错了insert into book(bookname,bookauthor,bookpress,bookedition,booklocation) values('啊实打实的','啊实打实',啊实打实,'啊实打实','撒大声地') com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column '???????‰“???' in 'field list'
4 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问