欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

学习使用jdbc向数据库中插入数据

程序员文章站 2022-05-17 18:50:59
...

add-book.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>添加图书信息</title>
</head>
<body>
	<p>请输入图书信息<P>
	${message}<br>
	<form action = "add-book"method="post">
		书号:<input type = "text"name="isbn"/><br>
		书名:<input type = "text"name="title"/><br>
		价格:<input type = "text"name="price"/><br>
		<input type = "submit"value= "确定"/><input type = "reset"value="重置"/>
	</form>

</body>
</html>

BookServlet.java

package com.demo;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

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 javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class BookServlet
 */
@WebServlet("/add-book")
public class BookServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	Connection dbconn = null;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public BookServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
		String driver = "com.mysql.cj.jdbc.Driver";
		String dburl = "jdbc:mysql://127.0.0.1:3306/webstore?serverTimezone=UTC";
		String username = "root";
		String password = "123456";
		try {
			Class.forName(driver);
			dbconn = DriverManager.getConnection(dburl,username,password);
			
		}catch(ClassNotFoundException e1) {
			System.out.println(e1);
			getServletContext().log("驱动程序类找不到!");
		}catch(SQLException e2) {
			System.out.println(e2);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String isbn = request.getParameter("isbn");
		String title = request.getParameter("title");
		float price = Float.parseFloat(request.getParameter("price"));
		String message="";
		
		try {
			String sql="INSERT INTO books VALUES(?,?,?)";
			PreparedStatement pstmt = dbconn.prepareStatement(sql);
			pstmt.setString(1, isbn);
			pstmt.setString(2,title);
			pstmt.setFloat(3, price);
			
			int num = pstmt.executeUpdate();
			if(num==1) {
				message = "插入记录成功!";
			}else {
				message = "插入记录失败!";
			}
			request.setAttribute("message", message);
			request.getRequestDispatcher("add-book.jsp").forward(request, response);
		}catch(SQLException e) {
			message = e.getMessage();
			request.setAttribute("message", message);
			request.getRequestDispatcher("add-book.jsp").forward(request, response);
		}
	}
	}


学习使用jdbc向数据库中插入数据