Shuo_Y 2021-01-01 23:34 采纳率: 0%
浏览 35

java+javabean+servlet+dao模式,某一行代码不报错也不运行

lib_insert.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>title</title>
  </head>
  
  <body align="center">
    <form action="Insert" method="post">
    	<table>
    		<tr>
    			<td>序号:</td>
    			<td><input type="text" name="id"></td>
    		</tr>
    		<tr>
    			<td>书名:</td>
    			<td><input type="text" name="name"></td>
    		</tr>
    		<tr>
    			<td>作者:</td>
    			<td><input type="text" name="author"></td>
    		</tr>
    		<tr>
    			<td>价格:</td>
    			<td><input type="text" name="price"></td>
    		</tr>
    		<tr>
    			<td>备注:</td>
    			<td><input type="text" name="note"></td>
    		</tr>
    		<tr>
    			<td><input type="submit" value="添加"></td>
    			<td><input type="reset" value="重置"></td>
    		</tr>
    	</table>
    </form>
  </body>
</html>

Insert.java

package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import beans.*;

public class Insert extends HttpServlet {

	public Insert() {
		super();
	}

	public void destroy() {
		super.destroy(); 
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		
		int id = Integer.parseInt(request.getParameter("id"));
		String name = request.getParameter("name");
		String author = request.getParameter("author");
		int price = Integer.parseInt(request.getParameter("price"));
		String note = request.getParameter("note");
		
		Library lib = new Library(id, name, author, price, note);
		LibraryDAO run = new LibraryDAO();
		Library li = null;
		
		try {
			System.out.print("++++++++++++");
			li = run.create(lib);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(li != null)
			request.getRequestDispatcher("/succeed.jsp").forward(request, response);
		else
			request.getRequestDispatcher("/error.jsp").forward(request, response);
	}

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

		doGet(request,response);
	}

	public void init() throws ServletException {
		// Put your code here
	}

}

libraryDao.java  部分代码

public Library create(Library lib) throws Exception {
		Connection con = null;
		PreparedStatement prepStmt = null;
		ResultSet rs = null;
		try{
			con = DbConnect.getDBconnection1();
			String sql = "insert into library_info(id, name, author, price, note)"+"value(?,?,?,?,?)";
			prepStmt = con.prepareStatement(sql);
			prepStmt.setInt(1, lib.getId());
			prepStmt.setString(2, lib.getName());
			prepStmt.setString(3, lib.getAuthor());
			prepStmt.setInt(4, lib.getPrice());
			prepStmt.setString(5, lib.getNote());
			prepStmt.executeUpdate();
		}catch(Exception e){
		}finally{
			DbConnect.closeDB(con, prepStmt, rs);
		}
		return lib;
	}

DbConnection.java

package beans;

import java.sql.*;

public class DbConnect {
	private static String driverName = "com.mysql.jdbc.Driver";
	private static String userName = "root";
	private static String userPwd = "891216";
	private static String dbName = "library_user";
	private static String dbName1 = "library_info";
	
	public static Connection getDBconnection1(){
		String url1 = "jdbc:mysql://localhost/"+dbName1;
		String url2 = "?user="+userName+"&password="+userPwd;
		String url3 = "&userUnicode=true&characterEncoding=UTF-8";
		String url = url1 + url2 + url3;
		try{
			Class.forName(driverName);
			Connection con = DriverManager.getConnection(url);
			return con;
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	
	public static void closeDB(Connection con, PreparedStatement pstm, ResultSet rs){
		try{
			if(rs != null) rs.close();
			if(pstm != null) pstm.close();
			if(con != null) con.close();
		}catch(SQLException e){
			e.printStackTrace();
		}
	}
}

没有运行的就是servlet的这一行,控制台中提示异常是没有创建数据库,但是我创建过了

  • 写回答

4条回答 默认 最新

  • 三石喽 2021-01-02 09:22
    关注

    是不是连接错数据库了,正常不会找不到的 

    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 虚心请教几个问题,小生先有礼了
  • ¥30 截图中的mathematics程序转换成matlab