学习使用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);
}
}
}
推荐阅读
-
android开发中如何使用jdbc连接数据库
-
使用JDBC的addBatch()方法提高数据库插入更新效率
-
Java开发中JDBC连接mysql数据库使用介绍
-
使用JDBC在MySQL数据库中快速批量插入数据_MySQL
-
客户每访问一次浏览器,就会向数据库B表中插入一条数据,求依次显示IP,用户登录次数,用户点击次数
-
运用INSERT INTO语句向数据库中插入数据失败。
-
java-向mysql数据库中插入数据时报错
-
在用mybatis向MySQL数据库中插入时间时报错:Incorrect datetime value: '' for column '' at row 1
-
mysql 向数据库中插入一项数据解决办法
-
sql向数据库表中插入列,sql给表的列添加说明,sql添加主外键约束