JDBC连接数据库添加数据

 

1.打开MyEclipse软件,创建Web project项目,首先创建添加用户页面,成功后跳转到另外一个页面,并将这些值写入数据库中,创建,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