zoukankan      html  css  js  c++  java
  • Java连接MYSQL进行操作(增,删,改)

    连接数据库,并用表格输出数据

    创建insex.jsp

    <table border="1" width="80%" align='center'>
    <tr>
    <td>序号</td>
    <td>学号</td>
    <td>姓名</td>
    <td>性别</td>
    <td>出生日期</td>
    <td>操作</td>
    </tr>
    <%
    int n=0;
    String driver_class="com.mysql.cj.jdbc.Driver";
    String serverip="localhost";
    String databasename="oa";
    String username="root";
    String pwd="123456";
    String jdbcUrl="jdbc:mysql://"+serverip+":3306/"+databasename+"?serverTimezone=Asia/Shanghai&useSSL=true";
    Class.forName(driver_class);
    Connection conn= DriverManager.getConnection(jdbcUrl,username,pwd);
    PreparedStatement pst= conn.prepareStatement("select * from student");
    ResultSet rs= pst.executeQuery();

    //ResultSet 一个指向数据库的变量,是不包含内容的

    while(rs.next()){
    n++;
    %>
    <tr>
    <td><%=n %></td>
    <td><%=rs.getString("id") %></td>
    <td><%=rs.getString("user_name") %></td>
    <td><%=rs.getString("sex") %></td>
    <td><%=rs.getString("birthday") %></td>
    <td>
    <a href="insert_action.jsp">添加</a>
    <a href="delete.jsp?id=<%=rs.getString("id")%>">删除</a>
    <a href="update_action.jsp?id=<%=rs.getString("id")%>&name=<%=rs.getString("user_name")%>&bir=<%=rs.getString("birthday")%>">修改</a></td>
    </tr>
    <%
    }

    pst.close();
    conn.close();
    %>
    </table>
    <%out.print("合计"+n+"记录"); %>

    运行结果如下:

    创建delete.jsp

    <%
    String id=request.getParameter("id");
    String driver_class="com.mysql.cj.jdbc.Driver";
    String serverip="localhost";
    String databasename="oa";
    String username="root";
    String pwd="123456";
    String jdbcUrl="jdbc:mysql://"+serverip+":3306/"+databasename+"?serverTimezone=Asia/Shanghai&useSSL=true";
    Class.forName(driver_class);
    Connection conn= DriverManager.getConnection(jdbcUrl,username,pwd);
    PreparedStatement pst1=conn.prepareStatement("delete from student where id=?");
    pst1.setString(1,id);
    pst1.execute();
    pst1.close();
    conn.close();

    response.sendRedirect("index.jsp");
    %>

    在之前的insex.jsp中已经调用过了

    运行后:从7条减为6条了

    创建insert_action.jsp

    <form action='insert.jsp' method="post">
    请输入姓名:
    <input type='text' name='username'>
    <br>
    请输入性别:
    <input name="radiobutton" type="radio" value="男" checked>

    <input type="radio" name="radiobutton" value="女">

    <br>
    请输入出生日期:
    <input type='text' name='birthday'>
    </p>
    <p>
    <input type="submit" name="submit" value='添加'>
    </p>
    </form>

    创建insert.jsp

    <%

    String name=request.getParameter("username");
    name=new String(name.getBytes("iso-8859-1"),"UTF-8");
    String sex=request.getParameter("radiobutton");
    sex=new String(sex.getBytes("iso-8859-1"),"UTF-8");
    String birthday=request.getParameter("birthday");

    String driver_class="com.mysql.cj.jdbc.Driver";
    String serverip="localhost";
    String databasename="oa";
    String username="root";
    String pwd="123456";
    String jdbcUrl="jdbc:mysql://"+serverip+":3306/"+databasename+"?serverTimezone=Asia/Shanghai&useSSL=true";
    Class.forName(driver_class);
    Connection conn= DriverManager.getConnection(jdbcUrl,username,pwd);
    PreparedStatement pst1=conn.prepareStatement("insert into student(user_name,sex,birthday)values(?,?,?)");
    pst1.setString(1,name);
    pst1.setString(2,sex);
    pst1.setString(3,birthday);
    pst1.execute();
    pst1.close();
    conn.close();

    response.sendRedirect("index.jsp");
    %>

    在之前的index.jsp中

    点击添加后

     输入数据再点击添加按钮

    可以看到数据添加成功了

    创建update_action.jsp

    <form action='update.jsp' method="post">
    <%
    String n=request.getParameter("id");
    String name=request.getParameter("name");
    String sex=request.getParameter("sex");
    String birthday=request.getParameter("bir");
    %>
    <input type='text' name='id'value='<%=n%>' style='display:none'>
    请输入姓名:
    <input type='text' name='username'value='<%=name%>'>
    <br>
    请输入性别:
    <script type="text/javascript">
    function loadsex(){
    <%
    if(sex.equals("男")){
    %>
    document.getElementById("radiobutton1").checked="checked";
    document.getElementById("radiobutton2").checked="";
    <%
    }else{
    %>
    document.getElementById("radiobutton1").checked="";
    document.getElementById("radiobutton2").checked="checked";
    <%
    }
    %>
    }
    onload=loadsex;
    </script>
    <input name="radiobutton" type="radio" value="男" id="radiobutton1" checked>

    <input type="radio" name="radiobutton" value="女" id="radiobutton2">

    <br>
    请输入出生日期:
    <input type='text' name='birthday'value='<%=birthday%>'>
    </p>
    <p>
    <input type="submit" name="submit" value='修改'>
    </p>
    </form>

    在创建update.jsp

    <%
    String id=request.getParameter("id");
    String name=request.getParameter("username");
    name=new String(name.getBytes("iso-8859-1"),"UTF-8");
    String sex=request.getParameter("radiobutton");
    sex=new String(sex.getBytes("iso-8859-1"),"UTF-8");
    String birthday=request.getParameter("birthday");
    String driver_class="com.mysql.cj.jdbc.Driver";
    String serverip="localhost";
    String databasename="oa";
    String username="root";
    String pwd="123456";
    String jdbcUrl="jdbc:mysql://"+serverip+":3306/"+databasename+"?serverTimezone=Asia/Shanghai&useSSL=true";
    Class.forName(driver_class);
    Connection conn= DriverManager.getConnection(jdbcUrl,username,pwd);
    PreparedStatement pst1=conn.prepareStatement("update student set user_name=?,sex=?,birthday=? where id=?;");
    pst1.setString(1,name);
    pst1.setString(2,sex);
    pst1.setString(3,birthday);
    pst1.setString(4,id);
    pst1.execute();
    pst1.close();
    conn.close();

    response.sendRedirect("index.jsp");
    %>

    在之前的index.jsp中

    点击修改后,出现修改前的数据,直接在文本框中修改即可

     

    把傻明改为明za,再点击修改按钮

  • 相关阅读:
    es3的语法来模拟es5的bind方法
    js判断变量的类型(使用闭包来玩一把)
    获取一组时间中的最近的日期
    可以设置超时版的的fetch
    错误排查
    如何查看Linux的CPU负载
    shell脚本操作mysql数据库,使用mysql的-e参数可以执行各种sql的(创建,删除,增,删,改、查)等各种操作
    Swoole server函数列表(转载)
    php的反射
    PHP共享内存
  • 原文地址:https://www.cnblogs.com/zym2000/p/9184673.html
Copyright © 2011-2022 走看看