データを追加するためのJDBC接続データベース

 

1. MyEclipseソフトウェアを開き、Webプロジェクトプロジェクトを作成し、最初にユーザーの追加ページを作成し、成功後に別のページにジャンプして、これらの値をデータベースに書き込み、作成、tianjia1.jsp、詳細なコードは次のとおりです。成功した追加ページ、tianjia2.jsp、詳細なコードは次のとおりです

<%@ 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.正常な追加ページtianjia2.jspを作成します。詳細なコードは、次のとおりです。

<%@ 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>

結果:

データの追加:

データの追加:

データベースの表示:

3.ページxiugai1.jsp4を作成および変更し、成功後に別のページにジャンプして、データベースの値を変更します。詳細なコードは次のとおりです。

<%@ 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.成功したページxiugai2.jspを作成して変更します。詳細なコードは、次のとおりです。

<%@ 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>

データの更新:

データベースの表示:

5.クエリページchaxun1.jspを作成し、成功したら別のページにジャンプして、ページにクエリ結果を表示します。詳細なコードは次のとおりです。

 

<%@ 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.正常なクエリページchaxun2.jspを作成します。詳細なコードは、次のとおりです。

<%@ 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>

結果:

クエリデータ:

7.ページshanchu1.jspを作成して削除します。成功したら、別のページプロンプトにジャンプして、データベースの値を削除します。詳細なコードは次のとおりです。

<%@ 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.正常に削除されたことを示すページshanchu2.jspを作成します。詳細なコードは、次のとおりです。

<%@ 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>

データを削除する

データベースの表示:

おすすめ

転載: blog.csdn.net/dengfengling999/article/details/123580847