栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 getAllStu() throws SQLException {
        List 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 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 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页面
//学生信息界面


学生信息

添加成员信息

<% List stus = s.getAllStu(); for (jdbc.Bean.Student ss:stus) { %> <% } %>
学号姓名性别年龄电话操 作
<%= ss.getStuNum()%> <%= ss.getStuName()%> <%= ss.getStuSex()%> <%= ss.getStuAge()%> <%= ss.getStuTel()%> ">修改   ">删除

学生信息界面.css代码

    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;
    }
  
 查询



    <%
        Connection cc= Dbutil.getConnection();
        String sql="select * from student";
        Statement stmt=cc.createStatement();
        ResultSet rs=stmt.executeQuery(sql);
        while (rs.next()){
    %>
    
    <%
        }
    %>
学号姓名性别年龄联系电话操作
<%=rs.getLong("stu_num")%> <%=rs.getString("stu_name")%> <%=rs.getString("stu_sex")%> <%=rs.getString("stu_age")%> <%=rs.getString("stu_tel")%> ">修改   ">删除
查询.css

        *{
            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;
        }
    
更新/修改





    updatestu




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

<%
    int num1=Integer.parseInt(request.getParameter("num"));
    sc.delStu(num1);
response.sendRedirect("DL2.jsp");
%>
写代码不易记得给个三连哦~

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

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

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/582188.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号