JDBC Implements Student Management System

  • 2021-06-29 11:14:01
  • OfStack

The specific code for JDBC to implement the student management system is shared for your reference, which is as follows:

1. Student Classes


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 Tool Class


package manage;
 
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
 
/**
 * @author fanxf
 * @since 2018/4/27 11:06
 */
// Tool classes for databases 
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;
 }
 // Release from small to large 
 //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. Code


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;
 
 /**
  *  Add Student Data 
  *
  * @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()); // Settings 1 Parameters 
   ps.setString(2, student.getSex()); // Settings 2 Parameters 
   ps.setString(3, student.getName()); // Settings 3 Parameters 
   result = ps.executeUpdate();
  } catch (SQLException e) {
   e.printStackTrace();
  } finally {
   JdbcUtils.release(null, ps, conn); // Close Connection 
  }
  return result;
 }
 
 public void add() {
  Scanner scan = new Scanner(System.in);
  System.out.println(" Please enter student age ");
  int age = scan.nextInt();
  System.out.println(" Please enter student gender ");
  String sex = scan.next();
  System.out.println(" Please enter a student name ");
  String name = scan.next();
  Student s = new Student(age, sex, name);
  int flag = addStudent(s);
  if (flag > 0) {
   System.out.println(" Added Successfully ");
  } else {
   System.out.println(" Failed to add ");
  }
 }
 
 /**
  *  modify 
  *
  * @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()); // Settings 1 Parameters 
   ps.setString(2, student.getSex()); // Settings 2 Parameters 
   ps.setString(3, student.getName()); // Settings 3 Parameters 
   ps.setInt(4, student.getId());
   result = ps.executeUpdate();
  } catch (SQLException e) {
   e.printStackTrace();
  } finally {
   JdbcUtils.release(null, ps, conn); // Close Connection 
  }
  return result;
 }
 
 public void update() {
  Scanner scan = new Scanner(System.in);
  System.out.println(" Please enter a student id");
  int id = scan.nextInt();
  System.out.println(" Please enter student age ");
  int age = scan.nextInt();
  System.out.println(" Please enter student gender ");
  String sex = scan.next();
  System.out.println(" Please enter a student name ");
  String name = scan.next();
  Student s = new Student(id, age, sex, name);
  int flag = updateStudent(s);
  if (flag > 0) {
   System.out.println(" Update Successful ");
  } else {
   System.out.println(" Update failed ");
  }
 }
 
 /**
  *  delete 
  *
  * @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); // Settings 1 Parameters 
   result = ps.executeUpdate();
  } catch (SQLException e) {
   e.printStackTrace();
  } finally {
   JdbcUtils.release(null, ps, conn); // Close Connection 
  }
  return result;
 }
 
 public void delete() {
  Scanner scan = new Scanner(System.in);
  System.out.println(" Please enter a student id");
  int id = scan.nextInt();
  int flag = deleteStudent(id);
  if (flag > 0) {
   System.out.println(" Delete succeeded ");
  } else {
   System.out.println(" Delete failed ");
  }
 }
 
 public static void main(String[] args) {
  System.out.println("************  Welcome to the Student Management System  *************");
  ManageSystem ms = new ManageSystem();
  boolean b = true;
  while (b) {
   System.out.println(" Which of the following do you want to do ");
   System.out.println("1 , Add Students  2 Update student data  3 , Student Information Query  4 Delete students  0 , Exit ");
   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(" No such action option , Please come over again! ");
     main(args);
     break;
   }
  }
 }
}

4. properties database file self-configuration

Database fields are built based on the student class!

For more learning materials, please pay attention to the theme "Management System Development".


Related articles: