jdbc+数据连接+增删改查+jsp页面

话不多说直接甩代码了,写了好几天终于写的差不多了~

jdbc.dao

package jdbc.dao;

import jdbc.Bean.Student;
import jdbc.Dbutil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class StudentDao {
 public List<Student> getAllStu() throws SQLException {
        List<Student> stus = new ArrayList<>();
        Connection conn = Dbutil.getConnection();
        String sql = "SELECT*FROM student";
        //接口对象怎么来
        Statement st = conn.createStatement();
        //接口对象通过什么方法执行sql
        ResultSet rs = st.executeQuery(sql);

        Student s = null;
        while (rs.next()) {
            s = new Student();
            s.setStuNum(rs.getLong("stu_num"));
            s.setStuName(rs.getString("stu_name"));
            s.setStuSex(rs.getString("stu_sex"));
            s.setStuAge(rs.getString("stu_age"));
            s.setStuTel(rs.getString("stu_tel"));
            stus.add(s);
        }
        return stus;
    }

    public Student getStuByNum(long num) throws SQLException {
        Student s = new Student();
        Connection conn = Dbutil.getConnection();
        String sql = "SELECT * FROM student WHERE stu_num= ?";
        PreparedStatement pst = conn.prepareStatement(sql);
        pst.setLong(1, num);
        ResultSet rs = pst.executeQuery();
        while (rs.next()) {
            s.setStuName(rs.getString("stu_name"));
            s.setStuSex(rs.getString("stu_sex"));
            s.setStuAge(rs.getString("stu_age"));
            s.setStuTel(rs.getString("stu_tel"));
            s.setStuNum(rs.getLong("stu_num"));
        }
        return s;
    }

    public Student getStuByName(String name) throws SQLException {
        Student s = new Student();
        Connection conn = Dbutil.getConnection();
        String sql = "SELECT * FROM student WHERE stu_name = ?";
        PreparedStatement pst = conn.prepareStatement(sql);
        pst.setString(1, name);
        ResultSet rs = pst.executeQuery();
        while (rs.next()) {
            s.setStuName(rs.getString("stu_name"));
            s.setStuSex(rs.getString("stu_sex"));
            s.setStuAge(rs.getString("stu_age"));
            s.setStuTel(rs.getString("stu_tel"));
            s.setStuNum(rs.getLong("stu_num"));
        }
        return s;
    }

    public void addStu(Student s) throws SQLException {
        Connection conn = Dbutil.getConnection();
        String sql = "INSERT INTO student VALUES(?,?,?,?,?)";//几个字段就有几个问号
        PreparedStatement pst = conn.prepareStatement(sql);
        pst.setLong(1, s.getStuNum());
        pst.setString(2, s.getStuName());
        pst.setString(3, s.getStuSex());
        pst.setString(4, s.getStuAge());
        pst.setString(5, s.getStuTel());
        pst.execute();
    }

    public void updateStu(Student s) throws SQLException {
        Connection conn = Dbutil.getConnection();
        String sql = "UPDATE  student SET stu_name= ?,stu_age=?,stu_sex=?,stu_tel=? " + "WHERE stu_num=?";//几个字段就有几个问号
        PreparedStatement pst = conn.prepareStatement(sql);
        pst.setString(5, s.getStuTel());
        pst.setLong(1, s.getStuNum());
        pst.setString(2, s.getStuName());
        pst.setString(3, s.getStuSex());
        pst.setString(4, s.getStuAge());
        pst.setString(5, s.getStuTel());
        int i = pst.executeUpdate();//返回修改条数
        System.out.println("修改了" + i + "条数据!");
    }

    public void delStu(long num) throws SQLException {
        Connection conn = Dbutil.getConnection();
        String sql = "DELETE FROM student WHERE stu_num= ?";//几个字段就有几个问号
        PreparedStatement pst = conn.prepareStatement(sql);
        pst.setLong(1, num);
        int rs = pst.executeUpdate();
        System.out.println("删除成功" + rs + "行受影响");
        conn.close();//关闭资源
    }


}

jdbc.Bean

package jdbc.Bean;

public class Student {
    private  long stuNum;
    private  String stuName;
    private  String stuSex;
    private  String stuAge;
    private  String stuTel;

    public Student(){

    }
    public Student(long stuNum,String stuName,String stuSex,String stuAge,String stuTel){
        this.stuNum=stuNum;
        this.stuName=stuName;
        this.stuSex=stuSex;
        this.stuAge=stuAge;
        this.stuTel=stuTel;
    }

    public long getStuNum() {
        return stuNum;
    }

    public void setStuNum(long stuNum) {
        this.stuNum = stuNum;
    }

    public String getStuName() {
        return stuName;
    }

    public void setStuName(String stuName) {
        this.stuName = stuName;
    }

    public String getStuSex() {
        return stuSex;
    }

    public void setStuSex(String stuSex) {
        this.stuSex = stuSex;
    }

    public String getStuAge() {
        return stuAge;
    }

    public void setStuAge(String stuAge) {
        this.stuAge = stuAge;
    }

    public void setStuTel(String stuTel) {
        this.stuTel = stuTel;
    }

    public String getStuTel() {
        return stuTel;
    }

    @Override
    public String toString() {
        return "Student{" +
                "stuNum=" + stuNum +
                ", stuName='" + stuName + '\'' +
                ", stuSex='" + stuSex + '\'' +
                ", stuAge='" + stuAge + '\'' +
                ", stuTel='" + stuTel + '\'' +
                '}';
    }


}

jdbc.Controller

package jdbc.Controller;

import jdbc.Bean.Student;
import jdbc.dao.StudentDao;

import java.sql.SQLException;
import java.util.List;

public class StuController {
     StudentDao sd;
    public List<Student> getAllStu()  throws SQLException{
        sd = new StudentDao();
        return sd.getAllStu();

    }

     public Student getStuByNum(long num){
        sd = new StudentDao();
        try {
            return sd.getStuByNum(num);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return null;
        }
    }

    public Student getStuByName(String name){
        sd = new StudentDao();
        try {
            return sd.getStuByName(name);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public void addStu(Student s){
        sd = new StudentDao();
        try {
            sd.addStu(s);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    public void delStu(long num){
        sd = new StudentDao();
        try {
            sd.delStu(num);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    public void updateStu(Student s){
        sd = new StudentDao();
        try {
            sd.updateStu(s);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    public static void main(String[] args) throws SQLException {
        StudentDao sd = new StudentDao();
        List<Student> stus = sd.getAllStu();
        for (Student s : stus) {
            System.out.println(s);
        }
    }
}

最重要的部分

jdbc.Dbutil

package jdbc;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;

public class Dbutil {
    static  String url="jdbc:mysql://localhost:3306/ruanjian2032";
    static  String userName="root";
    static  String password="123456";
     static Connection conn=null;

     public static Connection getConnection(){
         try{
             Class.forName("com.mysql.cj.jdbc.Driver");
             conn = DriverManager.getConnection(url,userName,password);
         }catch (Exception e){
             e.printStackTrace();
         }
         return conn;
     }
}

学生信息jsp页面

//学生信息界面
<body>

<h2 align="center">
  学生信息
  <br>
  <hr>

  <div id="search">
    <form method="post" action="min.jsp">
      <input id="inquires" type="search" name="num" placeholder="搜索/查询">
      <input id="sub" type="submit" value="查询">
    </form>
  </div>
  <table class="gridtable" align="center">
    <p id="p1"><a id="h" href="stuadd.jsp">添加成员信息</a></p>
    <tr>
      <th>学号</th><th>姓名</th><th>性别</th><th>年龄</th><th>电话</th><th>操 作</th>
    </tr>
    <jsp:useBean id="sc" class="jdbc.Controller.StuController"/>
    <jsp:useBean id="s" class="jdbc.dao.StudentDao"></jsp:useBean>
    <%
    List<jdbc.Bean.Student> stus =  s.getAllStu();
    for (jdbc.Bean.Student ss:stus) {
  %>
    <tr>
      <td><%= ss.getStuNum()%></td>
      <td><%= ss.getStuName()%></td>
      <td><%= ss.getStuSex()%></td>
      <td><%= ss.getStuAge()%></td>
      <td><%= ss.getStuTel()%></td>
      <td>
        <a href="stu.jsp?num=<%= ss.getStuNum()%>">修改</a>&nbsp;&nbsp;
        <a href="delete.jsp?num=<%= ss.getStuNum()%>">删除</a>
      </td>
    </tr>

    <%
      }
    %>
  </table>
</h2>
</body>

学生信息界面.css代码

<style>
    html{
      background: url("。。。。") no-repeat 0px 0px;
      color: paleturquoise;
    }
    table.gridtable {
      font-family: verdana,arial,sans-serif;
      font-size:11px;
      color:#333333;
      border-width: 1px;
      border-color: #666666;
      border-collapse: collapse;
      opacity: 0.6;
    }
    table.gridtable th {
      border-width: 1px;
      padding: 8px;
      border-style: solid;
      border-color: #666666;
      background-color: #dedede;
      opacity: 0.6;
    }
    table.gridtable td {
      border-width: 1px;
      padding: 8px;
      border-style: solid;
      border-color: #666666;
      background-color: #ffffff;
      opacity: 0.6;
    }
    input{
      opacity: 0.6;
    }
    p{
      opacity: 0.6;
    }
    #p1{
      opacity: 0.6;
      color: paleturquoise;
    }
  </style>

 查询

<jsp:useBean id="s" class="jdbc.dao.StudentDao"/>
<jsp:useBean id="ss" class="jdbc.Bean.Student"/>
<table>
    <tr>
        <th>学号</th><th>姓名</th><th>性别</th><th>年龄</th><th>联系电话</th><th>操作</th>
    </tr>
    <%
        Connection cc= Dbutil.getConnection();
        String sql="select * from student";
        Statement stmt=cc.createStatement();
        ResultSet rs=stmt.executeQuery(sql);
        while (rs.next()){
    %>
    <tr>
        <td><%=rs.getLong("stu_num")%></td>
        <td><%=rs.getString("stu_name")%></td>
        <td><%=rs.getString("stu_sex")%></td>
        <td><%=rs.getString("stu_age")%></td>
        <td><%=rs.getString("stu_tel")%></td>
        <td>
            <a href="update.jsp?num=<%= ss.getStuNum()%>">修改</a>&nbsp;&nbsp;
            <a href="delete.jsp?num=<%= ss.getStuNum()%>">删除</a>
        </td>
    </tr>
    <%
        }
    %>
</table>

查询.css

<style>
        *{
            margin: 0;
            padding: 0;
        }
        html{
            background: url("....") no-repeat 0px 0px;
           color: cyan;
        }
        table{
            border-collapse: collapse;
            width: 100%;
        }
        th, td{
            text-align: left;
            padding: 8px;
            opacity: 0.6;
        }
        tr:nth-child(even){
            background-color: #fafafa;
            opacity: 0.6;
        }
        th{
            background-color:#fbc2eb;
            color: white;
            opacity: 0.6;
        }
    </style>

更新/修改

<jsp:useBean id="stt" class="jdbc.Bean.Student"></jsp:useBean>
<jsp:useBean id="s" class="jdbc.Controller.StuController"></jsp:useBean>
<jsp:setProperty name="stt" property="*"></jsp:setProperty>
<html>
<head>
    <title>updatestu</title>
</head>

<body>

<%
    s.updateStu(stt);
    //    response.sendRedirect("DL2.jsp");
    request.getRequestDispatcher("DL2.jsp").forward(request,response);
%>
</body>

删除

<jsp:useBean id="sc" class="jdbc.Controller.StuController"></jsp:useBean>
<%
    int num1=Integer.parseInt(request.getParameter("num"));
    sc.delStu(num1);
response.sendRedirect("DL2.jsp");
%>
</body>

写代码不易记得给个三连哦~

第一篇文章写的不好的地方欢迎提供建议哦~

禁止转载一经发现后果自负

 

 

  • 4
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值