package com.xxu.java;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.sql.*;
/**
* 连接数据库的工具类
*
* @version 1.0
*/
public class DBUtil {
//连接的数据库的路径
private static final String URL = "jdbc:mysql://localhost:3306/student";
//数据库的账号
private static final String USERNAME = "root";
//数据库的密码
private static final String PASSWORD = "NYY250418";
private DBUtil() {
}
//通过静态代码块注册驱动
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 获得数据库连接
public static Connection getConnection() {
Connection conn = null;
try {
conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
// 根据id号进行查询
public static String findById(String id) {
String stuId = null;
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
int count = 0;
//获取连接
conn = DBUtil.getConnection();
//根据姓名查询
String sql = "select * from student_message where stu_id = ?";
ps = conn.prepareStatement(sql);
//给占位符赋值
ps.setString(1, id);
//执行SQL语句,返回结果集
rs = ps.executeQuery();
while (rs.next()) {
/*
将查询的结果给返回
如果查询到了返回id号
如果没有查询到,返回null
*/
stuId = rs.getString("stu_id");
}
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
closeConnection(rs, ps, conn);
}
return stuId;
}
// 添加同学信息
public static void addStudent(Student student) {
//获取数据库连接
Connection connection = DBUtil.getConnection();
// 生成一条sql语句
String sql = "insert into student_message (stu_id,stu_name,stu_sex,stu_age,stu_qq,stu_phone,stu_hobby,stu_email) values(?,?,?,?,?,?,?,?)";
PreparedStatement preparedStatement = null;
try {
//创建一个prepareStatement对象
preparedStatement = connection.prepareStatement(sql);
//为sql中的?赋值
preparedStatement.setString(1, student.getId());
preparedStatement.setString(2, student.getName());
preparedStatement.setString(3, student.getSex());
preparedStatement.setString(4, student.getAge());
preparedStatement.setString(5, student.getQQ());
preparedStatement.setString(6, student.getTel());
preparedStatement.setString(7, student.getHobby());
preparedStatement.setString(8, student.getEmail());
int count = preparedStatement.executeUpdate();
if (count == 1) {
new AddSuccess();
} else {
new AddDefeat();
}
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
if (connection != null) {
try {
connection.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
}
// 根据id删除学生
public static void deleteStudent(String id) {
//注册驱动
Connection conn = null;
PreparedStatement ps = null;
try {
conn = DBUtil.getConnection();
//要执行的SQL语句
String sql = "delete from student_message where stu_id = ?";
//预编译
ps = conn.prepareStatement(sql);
ps.setString(1, id);
int count = ps.executeUpdate();
if (count == 1) {
new DeleteSuccess();
} else {
new DeleteDefeatFrame();
}
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
if (ps != null) {
try {
ps.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
}
// 根据学号修改学生信息
public static void modifyStudent(Student student) {
Connection conn = null;
PreparedStatement ps = null;
conn = DBUtil.getConnection();
//根据id号进行修改
String sql = "update student_message set stu_name = ?,stu_sex = ?,stu_age = ?,stu_qq = ?,stu_phone = ?,stu_hobby = ?,stu_email = ? where stu_id = ?";
try {
ps = conn.prepareStatement(sql);
//为占位符赋值
ps.setString(1, student.getName());
ps.setString(2, student.getSex());
ps.setString(3, student.getAge());
ps.setString(4, student.getQQ());
ps.setString(5, student.getTel());
ps.setString(6, student.getHobby());
ps.setString(7, student.getEmail());
ps.setString(8, student.getId());
int count = ps.executeUpdate();
if (count == 1) {
new ModifySuccess();
} else {
new ModifyDefeat();
}
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
if (ps != null) {
try {
ps.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
}
//关闭数据库
public static void closeConnection(ResultSet rs, PreparedStatement pstmt, Connection conn) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
该同学录管理系统通过图形用户界面(GUI)简单实现界面效果展示,主要包含功能有对存储在数据库中的学生信息进行增删改查操作,该压缩包中已经包含对应的数据库表。 登录账号和密码在管理员类下面,可自行查看
资源推荐
资源详情
资源评论
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
共 20 条
- 1
![avatar](https://profile-avatar.csdnimg.cn/5431cc5908274eae82c191c1fd29392a_weixin_45773020.jpg!1)
Robort?
- 粉丝: 6
- 资源: 1
![benefits](https://csdnimg.cn/release/downloadcmsfe/public/img/vip-rights-1.c8e153b4.png)
下载权益
![privilege](https://csdnimg.cn/release/downloadcmsfe/public/img/vip-rights-2.ec46750a.png)
C知道特权
![article](https://csdnimg.cn/release/downloadcmsfe/public/img/vip-rights-3.fc5e5fb6.png)
VIP文章
![course-privilege](https://csdnimg.cn/release/downloadcmsfe/public/img/vip-rights-4.320a6894.png)
课程特权
![rights](https://csdnimg.cn/release/downloadcmsfe/public/img/vip-rights-icon.fe0226a8.png)
开通VIP
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)
- 1
- 2
前往页