java基于jdbc实现简单学生管理系统

网友投稿 235 2022-09-20


java基于jdbc实现简单学生管理系统

目录工具类工程目录:运行截图:

这个是java连接mysql数据库的一个简单学生系统,通过jdbc连接数据库。

工具类

JDBCuntils.

package Student;

import java.io.IOException;

import java.sql.*;

import java.util.Properties;

//数据库的工具类

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;

}

//释放的时候要从小到大释放

//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();

}

http://}

}

}

数据库配置文件(这个是连接你自己的数据库的信息,在包里创建就好)

db.properties

driver=com.mysql.jdbc.Driver

url=jdbc:mysql://localhost:3306/db3

user=root

password=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 + '\'' +

'}';

XUTpLxcu }

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 2018/4/27 17:06

*/

public class Studentmannger {

private static Connection conn = null;

private static PreparedStatement ps = null;

private static ResultSet rs = null;

/**

* 添加学生数据

*

* @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()); //设置第一个参数

ps.setString(2, student.getName()); //设置第二个参数

ps.setInt(3, student.getScore()); //设置第三个参数

result = ps.executeUpdate();

} catch (SQLException e) {

e.printStackTrace();

} finally {

JDBCuntils.release(null, ps, conn); //关闭连接

}

return result;

}

public void add() {

Scanner scan = new Scanner(System.in);

System.out.println("请输入学生学号");

int id = scan.nextInt();

System.out.println("请输入学生姓名");

String name = scan.next();

System.out.println("请输入学生成绩");

int score = scan.nextInt();

Student s = new Student(id, name, score);

int flag = addStudent(s);

if (flag > 0) {

System.out.println("添加成功");

} else {

System.out.println("添加失败");

}

}

/**1

*

* 修改

*

* @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()); //设置第一个参数

ps.setString(2, student.getName()); //设置第二个参数

ps.setInt(3, student.getScore()); //设置第三个参数

ps.setInt(4, student.getId());

result = ps.executeUpdate();

} catch (SQLException e) {

e.printStackTrace();

} finally {

JDBCuntils.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("请输入学生姓名");

scan.nextLine();

String name = scan.nextLine();

System.out.println("请输入学生成绩");

int score = scan.nextInt();

Student s = new Student(id, name, score );

int flag = updateStudent(s);

if (flag > 0) {

System.out.println("更新成功");

} else {

System.out.println("更新失败");

}

}

/**

* 删除

*

* @param id

* @return

* @throws SQLException

*/

public static void select() throws SQLException {

Scanner scan = new Scanner(System.in);

conn = JDBCuntils.getConnection();

int XUTpLxcun;

String sql = "select * from student where id=?";

PreparedStatement ps = conn.prepareStatement(sql);

System.out.println("请输入要查询的学号");

n = scan.nextInt();

ps.setInt(1, n);

ResultSet rs = ps.executeQuery();

//将sql语句传至数据库,返回的值为一个字符集用一个变量接收

while(rs.next()){ //next()获取里面的内容

System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getInt(3));

//getString(n)获取第n列的内容

//数据库中的列数是从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); //设置第一个参数

result = ps.executeUpdate();

} catch (SQLException e) {

e.printStackTrace();

} finally {

JDBCuntils.release(null, ps, conn); //关闭连接

}

return result;

}

public void delete() {

Scanner scan = new Scanner(System.in);

System.out.println("请输入学生id");

int id = scan.nextInt();

inXUTpLxcut flag = deleteStudent(id);

if (flag > 0) {

System.out.println("删除成功");

} else {

System.out.println("删除失败");

}

}

public static void main(String[] args) throws SQLException {

System.out.println("************ 欢迎进入学生管理系统 *************");

Studentmannger ms = new Studentmannger();

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:

ms.select();

break;

case 4:

ms.delete();

break;

default:

System.out.println("没有该操作选项,请重新来过!");

main(args);

break;

}

}

}

}

工程目录:

运行截图:


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Mirotik中VRF路由表中添加去往全局路由
下一篇:想翻倍加薪,网络工程师可考哪些证书用来提升自己?(考网络工程师有什么用)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~