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的这一行,控制台中提示异常是没有创建数据库,但是我创建过了
是不是连接错数据库了,正常不会找不到的
摆明就是数据库连接失败,不运行这行代码你怎么连接数据库?
链接数据库要写数据库的名称,你放个表名干嘛
这里你的数据库叫mysql