Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize simple Student Management system based on jdbc in java

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "how to implement a simple student management system based on java based on jdbc". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Catalogue

Tool class

Project catalogue:

Run the screenshot:

This is a simple student system that java connects to the mysql database and connects to the database through jdbc.

Tool class

JDBCuntils.

The utility class of package Student;import java.io.IOException;import java.sql.*;import java.util.Properties; / / database public class JDBCuntils {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 / / 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 ();}

Database configuration file (this is the information to connect to your own database, just create it in the package)

Db.properties

Driver=com.mysql.jdbc.Driverurl=jdbc:mysql://localhost:3306/db3user=rootpassword=1767737316.#initialSize=6#maxActive=20#minIdle=3#maxWait=60000

Student.java

Package Student;import java.util.Date;public class Student {private int id; private String name; private int score; public Student (int id, String name,int score) {this.id = id; this.name = name; this.score = score } public Student () {} public String toString () {return "Student {" + "name='" + id +'\'+ ", age=" + name + ", sex='" + score +'\'+'}' } public int getId () {return id;} public void setId (Integer id) {this.id = id;} public String getName () {return name;} public void setName (String name) {this.name = name;} public int getScore () {return score;} public void setScore (Integer score) {this.score = score }}

Studentmannger.java

Package Student;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.Scanner;/** * @ author fanxf * @ since 17:06 on 2018-4-27 * / public class Studentmannger {private static Connection conn = null; private static PreparedStatement ps = null; private static ResultSet rs = null / * add student data * * @ param student * @ return * @ throws SQLException * / public static int addStudent (Student student) {conn = JDBCuntils.getConnection (); int result = 0; try {ps = conn.prepareStatement ("INSERT INTO student (id, `name`, score) VALUES (?,?)") Ps.setInt (1, student.getId ()); / / set the first parameter ps.setString (2, student.getName ()); / / set the second parameter ps.setInt (3, student.getScore ()); / / set the third parameter result = ps.executeUpdate ();} catch (SQLException e) {e.printStackTrace () } finally {JDBCuntils.release (null, ps, conn); / / close connection} return result;} public void add () {Scanner scan = new Scanner (System.in); System.out.println ("Please enter student ID"); int id = scan.nextInt (); System.out.println ("Please enter student name") String name = scan.next (); System.out.println ("Please enter student scores"); int score = scan.nextInt (); Student s = new Student (id, name, score); int flag = addStudent (s); if (flag > 0) {System.out.println ("added successfully") } else {System.out.println ("add failed");}} / * 1 * * modify * * @ param student * @ return * / public static int updateStudent (Student student) {conn = JDBCuntils.getConnection (); int result = 0 Try {ps = conn.prepareStatement ("UPDATE student SET id =?, `name` =?, score =? WHERE id =? "); ps.setInt (1, student.getId ()); / / set the first parameter ps.setString (2, student.getName ()); / / set the second parameter ps.setInt (3, student.getScore ()); / / set the third parameter ps.setInt (4, student.getId ()); result = ps.executeUpdate () } catch (SQLException e) {e.printStackTrace ();} finally {JDBCuntils.release (null, ps, conn); / / close connection} return result;} public void update () {Scanner scan = new Scanner (System.in); System.out.println ("Please enter student id"); int id = scan.nextInt () System.out.println ("Please enter student name"); scan.nextLine (); String name = scan.nextLine (); System.out.println ("Please enter student score"); int score = scan.nextInt (); Student s = new Student (id, name, score); int flag = updateStudent (s) If (flag > 0) {System.out.println ("update successful");} else {System.out.println ("update failed") }} / * Delete * * @ param id * @ return * @ throws SQLException * / public static void select () throws SQLException {Scanner scan = new Scanner (System.in); conn = JDBCuntils.getConnection (); int n; String sql = "select * from student where id=?" PreparedStatement ps = conn.prepareStatement (sql); System.out.println ("Please enter the student number to query"); n = scan.nextInt (); ps.setInt (1, n); ResultSet rs = ps.executeQuery () / / pass the sql statement to the database, and the returned value is a character set. Use a variable to receive while (rs.next ()) {/ / next () to get the contents of System.out.println (rs.getInt (1) + "+ rs.getString (2) +" + rs.getInt (3)). / / getString (n) get the contents of the nth column / / the number of columns in the database is from 1}} public static int deleteStudent (int id) {conn = JDBCuntils.getConnection (); int result = 0 Try {ps = conn.prepareStatement ("DELETE FROM student WHERE id =?"); ps.setInt (1, id); / / set the first parameter result = ps.executeUpdate ();} catch (SQLException e) {e.printStackTrace ();} finally {JDBCuntils.release (null, ps, conn) / / close connection} return result;} public void delete () {Scanner scan = new Scanner (System.in); System.out.println ("Please enter student id"); int id = scan.nextInt (); int flag = deleteStudent (id); if (flag > 0) {System.out.println ("deleted successfully") } else {System.out.println ("deletion failed");}} public static void main (String [] args) throws SQLException {System.out.println ("* Welcome to the student management system *"); Studentmannger ms = new Studentmannger (); boolean b = true While (b) {System.out.println ("which of the following do you want to do"); System.out.println ("1, add student 2, update student data 3, student information query 4, delete student 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: ms.select (); break Case 4: ms.delete (); break; default: System.out.println ("there is no option for this operation, please start over!") ; main (args); break;} project directory:

Run the screenshot:

This is the end of the introduction of "how to implement a simple student management system based on java based on jdbc". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report