<sup id="sjju3"><noscript id="sjju3"></noscript></sup>
    <big id="sjju3"></big>

  • <blockquote id="sjju3"></blockquote>
    <blockquote id="sjju3"></blockquote>

      <td id="sjju3"></td>

      <big id="sjju3"></big>
        <code id="sjju3"><strong id="sjju3"><dl id="sjju3"></dl></strong></code>
      1. JDBC实现学生管理系统

         更新时间:2019年02月20日 10:42:15   作者:feidao0   我要评论

        这篇文章主要为大家详细介绍了JDBC实现学生管理系统,具有一定的参考价值,感兴趣的小伙伴们可以参?#23478;?#19979;

        本文实例为大家分享了JDBC实现学生管理系统的具体代码,供大家参考,具体内容如下

        1、学生类

        package manage;
         
        import java.util.Date;
         
        /**
         * @author fanxf
         * @since 2018/4/27 17:01
         */
        public class Student {
         
          private int id;
         
          private int age;
         
          private String sex;
         
          private String name;
         
          private Date dateCreated;
         
          public int getId() {
            return id;
          }
         
          public void setId(int id) {
            this.id = id;
          }
         
          public int getAge() {
            return age;
          }
         
          public void setAge(int age) {
            this.age = age;
          }
         
          public String getSex() {
            return sex;
          }
         
          public void setSex(String sex) {
            this.sex = sex;
          }
         
          public String getName() {
            return name;
          }
         
          public void setName(String name) {
            this.name = name;
          }
         
          public Date getDateCreated() {
            return dateCreated;
          }
         
          public void setDateCreated(Date dateCreated) {
            this.dateCreated = dateCreated;
          }
         
          public Student() {
          }
         
          public Student(int age, String sex, String name) {
            this.age = age;
            this.sex = sex;
            this.name = name;
          }
         
          public Student(int id, int age, String sex, String name) {
            this.id = id;
            this.age = age;
            this.sex = sex;
            this.name = name;
          }
         
          @Override
          public String toString() {
            return "Student{" +
                "id=" + id +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                ", name='" + name + '\'' +
                ", dateCreated=" + dateCreated +
                '}';
          }
        }

        2、jdbc工具类

        package manage;
         
        import java.io.IOException;
        import java.sql.*;
        import java.util.Properties;
         
        /**
         * @author fanxf
         * @since 2018/4/27 11:06
         */
        //数据库的工具类
        public class JdbcUtils {
         
          private static String driver = "";
          private static String url = "";
          private static String user = "";
          private static String password = "";
         
          static {
            Properties p = new Properties();
            try {
              p.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties"));
            } catch (IOException e) {
              e.printStackTrace();
            }
            driver = p.getProperty("driver");
            url = p.getProperty("url");
            user = p.getProperty("user");
            password = p.getProperty("password");
            try {
              Class.forName(driver);
            } catch (ClassNotFoundException e) {
              e.printStackTrace();
            }
          }
         
          public static Connection getConnection() {
            try {
              return DriverManager.getConnection(url, user, password);
            } catch (SQLException e) {
              e.printStackTrace();
            }
            return null;
          }
          //释放的时候要?#26377;?#21040;大释放
          //Connection -> Statement --> Resultset
         
         
          public static void release(ResultSet rs, Statement stmt, Connection conn) {
            if (rs != null) {
              try {
                rs.close();
              } catch (SQLException e) {
                e.printStackTrace();
              }
            }
            if (stmt != null) {
              try {
                stmt.close();
              } catch (SQLException e) {
                e.printStackTrace();
              }
            }
         
            if (conn != null) {
              try {
                conn.close();
              } catch (SQLException e) {
                e.printStackTrace();
              }
            }
          }
        }

        3、代码

        package manage;
         
        import java.sql.Connection;
        import java.sql.PreparedStatement;
        import java.sql.ResultSet;
        import java.sql.SQLException;
        import java.util.Scanner;
         
        /**
         * @author fanxf
         * @since 2018/4/27 17:06
         */
        public class ManageSystem {
         
          private static Connection conn = null;
          private static PreparedStatement ps = null;
          private static ResultSet rs = null;
         
          /**
           * 添加学生数据
           *
           * @param student
           * @return
           */
          public static int addStudent(Student student) {
            conn = JdbcUtils.getConnection();
            int result = 0;
            try {
              ps = conn.prepareStatement("INSERT INTO student (age, sex, `name`, dateCreated) VALUES (?, ?, ?, now())");
              ps.setInt(1, student.getAge()); //设置第一个参数
              ps.setString(2, student.getSex()); //设置第二个参数
              ps.setString(3, student.getName()); //设置第三个参数
              result = ps.executeUpdate();
            } catch (SQLException e) {
              e.printStackTrace();
            } finally {
              JdbcUtils.release(null, ps, conn); //关闭连接
            }
            return result;
          }
         
          public void add() {
            Scanner scan = new Scanner(System.in);
            System.out.println("请输入学生年龄");
            int age = scan.nextInt();
            System.out.println("请输入学生性别");
            String sex = scan.next();
            System.out.println("请输入学生姓名");
            String name = scan.next();
            Student s = new Student(age, sex, name);
            int flag = addStudent(s);
            if (flag > 0) {
              System.out.println("添加成功");
            } else {
              System.out.println("添加失败");
            }
          }
         
          /**
           * 修改
           *
           * @param student
           * @return
           */
          public static int updateStudent(Student student) {
            conn = JdbcUtils.getConnection();
            int result = 0;
            try {
              ps = conn.prepareStatement("UPDATE student SET age = ?, sex = ?, `name` = ? WHERE id = ?");
              ps.setInt(1, student.getAge()); //设置第一个参数
              ps.setString(2, student.getSex()); //设置第二个参数
              ps.setString(3, student.getName()); //设置第三个参数
              ps.setInt(4, student.getId());
              result = ps.executeUpdate();
            } catch (SQLException e) {
              e.printStackTrace();
            } finally {
              JdbcUtils.release(null, ps, conn); //关闭连接
            }
            return result;
          }
         
          public void update() {
            Scanner scan = new Scanner(System.in);
            System.out.println("请输入学生id");
            int id = scan.nextInt();
            System.out.println("请输入学生年龄");
            int age = scan.nextInt();
            System.out.println("请输入学生性别");
            String sex = scan.next();
            System.out.println("请输入学生姓名");
            String name = scan.next();
            Student s = new Student(id, age, sex, name);
            int flag = updateStudent(s);
            if (flag > 0) {
              System.out.println("更新成功");
            } else {
              System.out.println("更新失败");
            }
          }
         
          /**
           * 删除
           *
           * @param id
           * @return
           */
          public static int deleteStudent(int id) {
            conn = JdbcUtils.getConnection();
            int result = 0;
            try {
              ps = conn.prepareStatement("DELETE FROM student WHERE id = ?");
              ps.setInt(1, id); //设置第一个参数
              result = ps.executeUpdate();
            } catch (SQLException e) {
              e.printStackTrace();
            } finally {
              JdbcUtils.release(null, ps, conn); //关闭连接
            }
            return result;
          }
         
          public void delete() {
            Scanner scan = new Scanner(System.in);
            System.out.println("请输入学生id");
            int id = scan.nextInt();
            int flag = deleteStudent(id);
            if (flag > 0) {
              System.out.println("删除成功");
            } else {
              System.out.println("删除失败");
            }
          }
         
          public static void main(String[] args) {
            System.out.println("************ 欢迎进入学生管理系统 *************");
            ManageSystem ms = new ManageSystem();
            boolean b = true;
            while (b) {
              System.out.println("你想进行以下哪项操作");
              System.out.println("1、添加学生  2、更新学生数据  3、学生信息查询  4、删除学生 0、退出");
              Scanner scan = new Scanner(System.in);
              int i = scan.nextInt();
              switch (i) {
                case 1:
                  ms.add();
                  break;
                case 2:
                  ms.update();
                  break;
                case 3:
                  System.out.println();
                  break;
                case 4:
                  ms.delete();
                  break;
                default:
                  System.out.println("没有该操作选项,请重新来过!");
                  main(args);
                  break;
              }
            }
          }
        }

        4、properties数据库文件自?#21495;?#32622;

        数据库字段根据学生类建立!

        以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

        相关文章

        最新评论

        2018白小姐一肖中特马
        <sup id="sjju3"><noscript id="sjju3"></noscript></sup>
        <big id="sjju3"></big>

      2. <blockquote id="sjju3"></blockquote>
        <blockquote id="sjju3"></blockquote>

          <td id="sjju3"></td>

          <big id="sjju3"></big>
            <code id="sjju3"><strong id="sjju3"><dl id="sjju3"></dl></strong></code>
          1. <sup id="sjju3"><noscript id="sjju3"></noscript></sup>
            <big id="sjju3"></big>

          2. <blockquote id="sjju3"></blockquote>
            <blockquote id="sjju3"></blockquote>

              <td id="sjju3"></td>

              <big id="sjju3"></big>
                <code id="sjju3"><strong id="sjju3"><dl id="sjju3"></dl></strong></code>
              1. 吉林十一选五遗漏 安徽排列5大奖 免费一码中特已公开 七星彩近十期开奖号码 广东彩票资讯网 千禧3d开机号关注号 极速11选5是哪办的 湖北11选5前三直中大奖 排列三走势图表 甘肃11选5走势图遗漏分布 体彩6+1玩法 3d黑彩组三怎么赔率 上海快三开奖最新走势图 123tk六合图库大全 2019彩票中奖排名