以MySQL为后台的学生数据操作实现
参考相关资料
MySQL下载和安装配置
navicat下载安装和激活
使用navicat连接mysql数据库
小吐槽
-
Win10的快捷键Win+R不是以管理员身份运行,而MySQL一定得管理员身份进入
连接数据库
点击查看代码
public Mysqld(String account,String password) {
//定义一个数据库的账号和密码
//第一步加载驱动
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("加载驱动成功");
} catch (Exception e) {
// TODO: handle exception
System.out.println("加载驱动失败");
}
//
String uri="jdbc:mysql://localhost:3306/student?characterEncoding=utf-8&useSSL=false";
try {
con=DriverManager.getConnection(uri,account,password);
System.out.println("数据库连接成功");
} catch (SQLException e) {
// TODO: handle exception
System.out.println("数据库连接失败");
}
}
以管理员身份登录
点击查看代码
public static boolean LoginAccount(String account,String password) {
Statement sql;//用来执行命令的
ResultSet rs;//定义一个结果
String uri = "select account,password from user";
try {
sql = con.createStatement();
rs = sql.executeQuery(uri);
while(rs.next()) {
String acc=rs.getString(1);
String pass=rs.getString(2);
if(acc.equals(account)&&pass.equals(password)) {
return true;
}
}
} catch (Exception e) {
// TODO: handle exception
}
return false;
}
添加学生信息
点击查看代码
public static void addStudent(String name,String gender,String id,String birthday,String politicaloutlook,String ad,String tel,String number) {
PreparedStatement presql;
String sqlstr="insert into stu(name,gender,id,birthday,politicaloutlook,ad,tel,number)values(?,?,?,?,?,?,?,?)";
try {
presql=con.prepareStatement(sqlstr);
presql.setString(1, name);
presql.setString(2, gender);
presql.setString(3, id);
presql.setString(4, birthday);
presql.setString(5, politicaloutlook);
presql.setString(6, ad);
presql.setString(7, tel);
presql.setString(8, number);
presql.executeUpdate();
JOptionPane.showMessageDialog(null, "添加成功", "添加数据", JOptionPane.WARNING_MESSAGE);
String[] data = new String[8];
data[0] = name;
data[1] = gender;
data[2] = id;
data[3] = birthday;
data[4] = politicaloutlook;
data[5] = ad;
data[6] = tel;
data[7] = number;
Maneger.model.addRow(data);
} catch (Exception e) {
// TODO: handle exception
JOptionPane.showMessageDialog(null, "当前学号已经存在", "添加数据", JOptionPane.WARNING_MESSAGE);
}
}
删除学生信息
点击查看代码
public static void delStudent(String id) {
PreparedStatement presql;
String sqlstr="delete from stu where id=?";
try {
presql=con.prepareStatement(sqlstr);
presql.setString(1, id);
int ok=presql.executeUpdate();
if(ok==0) {
JOptionPane.showMessageDialog(null, "未找到相关学号", "删除数据", JOptionPane.WARNING_MESSAGE);
}else {
JOptionPane.showMessageDialog(null, "删除成功", "删除数据", JOptionPane.WARNING_MESSAGE);
}
} catch (Exception e) {
// TODO: handle exception
JOptionPane.showMessageDialog(null, "当前学号已经存在", "添加数据", JOptionPane.WARNING_MESSAGE);
}
}
修改学生信息
点击查看代码
public static void changeStudent(String name,String gender,String id,String birthday,String politicaloutlook,String ad,String tel,String number) {
PreparedStatement presql;
String sqlstr="delete from stu where id=?";
try {
presql=con.prepareStatement(sqlstr);
presql.setString(1, id);
int ok=presql.executeUpdate();
if(ok==0) {
JOptionPane.showMessageDialog(null, "未找到相关学号", "删除数据", JOptionPane.WARNING_MESSAGE);
}else {
sqlstr="insert into stu(name,gender,id,birthday,politicaloutlook,ad,tel,number)values(?,?,?,?,?,?,?,?)";
presql=con.prepareStatement(sqlstr);
presql.setString(1, name);
presql.setString(2, gender);
presql.setString(3, id);
presql.setString(4, birthday);
presql.setString(5, politicaloutlook);
presql.setString(6, ad);
presql.setString(7, tel);
presql.setString(8, number);
ok=presql.executeUpdate();
JOptionPane.showMessageDialog(null, "更改成功", "更改数据", JOptionPane.WARNING_MESSAGE);
String[] data = new String[8];
data[0] = name;
data[1] = gender;
data[2] = id;
data[3] = birthday;
data[4] = politicaloutlook;
data[5] = ad;
data[6] = tel;
data[7] = number;
Maneger.model.addRow(data);
}
} catch (Exception e) {
// TODO: handle exception
JOptionPane.showMessageDialog(null, "更改失败", "更改数据", JOptionPane.WARNING_MESSAGE);
}
}
查看学生信息
点击查看代码
public static void showAllStudent() {
PreparedStatement presql;
String sqlstr="select * from stu";
try {
presql = con.prepareStatement(sqlstr);
ResultSet rs = presql.executeQuery();
LoginEvent.addList(rs);
}catch(SQLException e) {
}
}
public static void showStudent(String id) {
PreparedStatement presql;
String sqlstr="select * from stu";
try {
presql = con.prepareStatement(sqlstr);
ResultSet rs = presql.executeQuery();
while(rs.next()) {
if(rs.getString(2).equals(id)) {
String[] data = new String[8];
data[0] = rs.getString(1);
data[1] = rs.getString(3);
data[2] = rs.getString(2);
data[3] = rs.getString(4);
data[4] = rs.getString(5);
data[5] = rs.getString(6);
data[6] = rs.getString(7);
data[7] = rs.getString(8);
Maneger.model.addRow(data);
}
}
}catch(SQLException e) {
}
}
}