JDBC connection database to add data

 

1. Open MyEclipse software, create a Web project project, first create an add user page, jump to another page after success, and write these values ​​into the database, create, tianjia1.jsp, the detailed code is as follows: Create a successful add page, tianjia2.jsp, the detailed code is as follows

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
     <form action="tianjia2.jsp" method="post">
        <table>
       <caption>注册页面</caption>
         <tr>
           <td>输入id:
           </td>
           <td>
             <input name="id" type="text">
           </td>
         </tr>
         <tr>
           <td>输入姓名:
           </td>
           <td>
             <input name="name" type="text">
           </td>
         </tr>
         <tr>
           <td>性别:
           </td>
           <td>
             <input name="sex" type="radio" value="男" checked>男
             <input name="sex" type="radio" value="女" >女
           </td>
         </tr>
         <tr>
           <td>提交
           </td>
           <td>
             <input type="submit" value="注册">
           </td>
         </tr>
     </table>
     </form>
  </body>
</html>

2. Create a successful addition page, tianjia2.jsp, the detailed code is as follows:

<%@ page language="java" import="java.util.*" import="java.sql.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'tianjia.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <%
      request.setCharacterEncoding("UTF-8");
      String id=request.getParameter("id");
      String name=request.getParameter("name");
      String sex=request.getParameter("sex");
      
      Class.forName("com.mysql.jdbc.Driver");//加载驱动
      Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/user?useUnicode=true&characterEncoding=utf-8","root","123456");
      String s1="insert into student values(?,?,?)";
      PreparedStatement ps=con.prepareStatement(s1);
      ps.setString(1, id);
      ps.setString(2, name);
      ps.setString(3, sex);
      
      int i=ps.executeUpdate();
      out.print("添加成功"+i+"行");
      
      ps.close();
      con.close();//关闭数据库连接
      
     %>
    
  </body>
</html>

result:

adding data:

adding data:

View database:

3. Create and modify the page xiugai1.jsp4, jump to another page after success, and modify the value in the database. The detailed code is as follows:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'xiugai1.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
     <form action="xiugai2.jsp" method="post">
        <table>
       <caption>修改页面</caption>
         <tr>
           <td>请输入要修改用户的id:
           </td>
           <td>
             <input name="id" type="text">
           </td>
         </tr>
         <tr>
           <td>请输入要修改用户后的姓名:
           </td>
           <td>
             <input name="name" type="text">
           </td>
         </tr>
         <tr>
           
           <td>
             <input type="submit" value="修改">
           </td>
         </tr>
     </table>
     </form>
  </body>
</html>

 4. Create and modify the successful page, xiugai2.jsp, the detailed code is as follows:

<%@ page language="java" import="java.util.* " import="java.sql.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'xiugai2.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
     <%
       request.setCharacterEncoding("UTF-8");
      String id=request.getParameter("id");
     String name=request.getParameter("name");
      
      Class.forName("com.mysql.jdbc.Driver");//加载驱动
      Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/user?useUnicode=true&characterEncoding=utf-8","root","123456");
      String s1="update student set name=? where id=?";
      PreparedStatement ps=con.prepareStatement(s1);
      
      ps.setString(1, name);
      ps.setString(2, id);
     
      int i=ps.executeUpdate();
      out.print("成功修改"+i+"行");
     
      ps.close();
      con.close();//关闭数据库连接
     %>
  </body>
</html>

update data:

View database:

5. Create a query page chaxun1.jsp, jump to another page after success, and display the query result on the page. The detailed code is as follows:

 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'chaxun1.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <form action="chaxun2.jsp" method="post">
        <table>
       <caption>查询页面</caption>
         <tr>
           <td>输入查询的姓名:
           </td>
           <td>
             <input name="name" type="text">
           </td>
         </tr>
         <tr>
           
           <td>
             <input type="submit" value="查询">
           </td>
         </tr>
     </table>
     </form>
  </body>
</html>

6. Create a successful query page, chaxun2.jsp, the detailed code is as follows:

<%@ page language="java" import="java.util.*" import="java.sql.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'chaxun2.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <%
       request.setCharacterEncoding("UTF-8");
      String name=request.getParameter("name");
      
      Class.forName("com.mysql.jdbc.Driver");//加载驱动
      Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/user?useUnicode=true&characterEncoding=utf-8","root","123456");
      String s1="select * from student where name=?";
      PreparedStatement ps=con.prepareStatement(s1);
      ps.setString(1, name);
      
      ResultSet rs=ps.executeQuery();
      while(rs.next()){
         int id=rs.getInt(1);
         String name1=rs.getString(2);
         String sex=rs.getString(3);
         out.print(id);out.println();
         out.print(name1);out.println();
         out.print(sex);out.println();
      }
      ps.close();
      con.close();//关闭数据库连接
       
     %>
  </body>
</html>

result:

Query data:

7. Create and delete the page shanchu1.jsp. After success, jump to another page prompt to delete the value in the database. The detailed code is as follows:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'shanchu1.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <body>
     <form action="shanchu2.jsp" method="post">
        <table>
       <caption>删除页面</caption>
         <tr>
           <td>输入删除id:
           </td>
           <td>
             <input name="id" type="text">
           </td>
         </tr>
         <tr>
           
           <td>
             <input type="submit" value="删除">
           </td>
         </tr>
     </table>
     </form>
  </body>
  </body>
</html>

 8. Create a page showing successful deletion, shanchu2.jsp, the detailed code is as follows:

<%@ page language="java" import="java.util.*" import="java.sql.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'shanchu2.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <%
       request.setCharacterEncoding("UTF-8");
      String id=request.getParameter("id");
     
      
      Class.forName("com.mysql.jdbc.Driver");//加载驱动
      Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/user?useUnicode=true&characterEncoding=utf-8","root","123456");
      String s1="delete from student where id=?";
      PreparedStatement ps=con.prepareStatement(s1);
      ps.setString(1, id);
     
      int i=ps.executeUpdate();
      out.print("成功删除"+i+"行");
      
      ps.close();
      con.close();//关闭数据库连接
     %>
  </body>
</html>

delete data

View database:

Guess you like

Origin blog.csdn.net/dengfengling999/article/details/123580847