时间:2021-07-01 10:21:17 帮助过:2人阅读
上面代码中 user 和 password 是你自己数据库的用户名和密码
127.0.0.1也是看你自己数据库的地址
我的两个数据库连接 localhost 和127.0.0.1都一样
运行到服务器上会显示如图结果
以上就是查询mySQL的部分
接下来插入新数据
新建InsertDate.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>Insert title here</title>
</head>
<body>
<form action="./insertDate" method="post">
id: <input type="text" name="id" /><br/>
bookname: <input type="text" name="bookname" /><br/>
publishing:<input type="text" name="publishing" /><br/>
writer: <input type="text" name="writer" /><br/>
price: <input type="text" name="price" /><br/>
date: <input type="text" name="date" /><br/>
<input type="submit" value="submit"/>
</form>
</body>
</html>
其中form的action insertDate是一个servlet 负责插入数据库数据
新建这个servlet
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
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 insertDate
*/
@WebServlet("/insertDate")
public class insertDate extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public insertDate() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
PrintWriter out = response.getWriter();
String id = request.getParameter("id");
String bookname = request.getParameter("bookname");
String publishing = request.getParameter("publishing");
String writer = request.getParameter("writer");
String price = request.getParameter("price");
String date=request.getParameter("date");
String driverClass="com.mysql.jdbc.Driver";
String url = "jdbc:mysql://127.0.0.1/javatemp";
String user = "root";
String password = "1234";
Connection conn;
try {
int i=0;
Class.forName(driverClass).newInstance();
conn = DriverManager.getConnection(url,user,password);
String sql = "insert into new_table (id,bookname,publishing,writer,price,date) "
+ "values(?,?,?,?,?,?)";
Connection conn1 = DriverManager.getConnection(url, user, password);
PreparedStatement pstmt;
pstmt = (PreparedStatement) conn1.prepareStatement(sql);
pstmt.setString(1, id);
pstmt.setString(2, bookname);
pstmt.setString(3, publishing);
pstmt.setString(4, writer);
pstmt.setString(5, price);
pstmt.setString(6, date);
i = pstmt.executeUpdate();
conn1.close();
pstmt.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
运行之后 插入数据注意数据类型 id不能重复 date是DATE类型
格式为 年-月-日
java web 中 JSP增删改查mySQL数据
标签: