java web 中 JSP增删改查mySQL数据

先在workbench中新建一个schema 比如叫 javatemp
再右键shcema里面的table新建一个叫new_table的table
然后设置一下table里面有什么 如图
java web 中 JSP增删改查mySQL数据_第1张图片
右键new_table 显示前100行 可以先往里面手动添加一些数据
这里写图片描述
数据库的就到这

然后打开J2EE 新建Dynamic web project
先导入连接mysql的驱动jar包 直接将jar包拖到WEB-INF下的lib目录
java web 中 JSP增删改查mySQL数据_第2张图片
在webContent里新建读取数据库jsp文件 代码如下


<%@page import="java.sql.*"%>

<%@ 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>JSP读取数据库</title>
</head>
<body>
<table border="1" align="center">
    <tr>
        <th>id</th>
        <th>name</th>
        <th>author</th>
        <th>publishing</th>
        <th>price</th>
        <th>data</th>
    </tr>
    <% String driverClass="com.mysql.jdbc.Driver"; String url = "jdbc:mysql://127.0.0.1/javatemp"; String user = "root"; String password = "1234"; Connection conn; PreparedStatement pst = null; try{ Class.forName(driverClass).newInstance(); conn = DriverManager.getConnection(url,user,password); String sql = "select * from new_table"; Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql); while(rs.next()){ %>
        <tr>
            <td><%=rs.getString("id") %></td>
            <td><%=rs.getString("bookname") %></td>
            <td><%=rs.getString("publishing") %></td>
            <td><%=rs.getString("writer") %></td>
            <td><%=rs.getString("price") %></td>
            <td><%=rs.getString("date") %></td>
        </tr>
        <% } }catch(Exception e){ e.printStackTrace(); } %>
</table>
</body>
</html>

上面代码中 user 和 password 是你自己数据库的用户名和密码
127.0.0.1也是看你自己数据库的地址
java web 中 JSP增删改查mySQL数据_第3张图片
我的两个数据库连接 localhost 和127.0.0.1都一样
运行到服务器上会显示如图结果
java web 中 JSP增删改查mySQL数据_第4张图片
以上就是查询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)