Insert 添加函数功能的实现
package Student_Manage_System;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.swing.JOptionPane;
public class Insert {
public static void Inser_toinfo(String id,String name
,String sex,int age,String date,String major,int classid
,String address,String tel) throws Exception{
Connection con=DBUtil.getCon("studentdb");
String sql="insert into student_info values(?,?,?,?,?,?,?,?,?)";
PreparedStatement prep=con.prepareStatement(sql);
prep.setString(1, id);
prep.setString(2, name);
prep.setString(3,sex);
prep.setInt(4, age);
prep.setString(5,date);
prep.setString(6, major);
prep.setInt(7, classid);
prep.setString(8, address);
prep.setString(9, tel);
prep.executeUpdate();
System.out.print("学生信息插入成功!");
JOptionPane.showMessageDialog(null, "学生信息插入成功!");
con.close();
}
public static void Inser_toscore(String id,String name,String major
,int classid,String math,String java,String mysql) throws Exception{
Connection con=DBUtil.getCon("studentdb");
String sql="insert into student_score values(?,?,?,?,?,?,?)";
PreparedStatement prep=con.prepareStatement(sql);
prep.setString(1, id);
prep.setString(2, name);
prep.setString(3, major);
prep.setInt(4, classid);
prep.setString(5, math);
prep.setString(6, java);
prep.setString(7, mysql);
prep.executeUpdate();
System.out.print("学生成绩插入成功!");
JOptionPane.showMessageDialog(null, "学生成绩插入成功!");
con.close();
}
}
Delete 删除功能函数的实现
package Student_Manage_System;
import java.sql.Connection;
import java.sql.PreparedStatement;
import javax.swing.JOptionPane;
public class Delete {
public static void Delete_toinfo(String id,String name) throws Exception{
Connection con=DBUtil.getCon("studentdb");
String sql="delete from student_info where ID=? and Name=?";
PreparedStatement prep=con.prepareStatement(sql);
prep.setString(1, id);
prep.setString(2, name);
prep.executeUpdate();
System.out.println("学生信息删除成功");
JOptionPane.showMessageDialog(null, "学生信息删除成功!");
con.close();
}
public static void Delete_toscore(String id,String name) throws Exception{
Connection con=DBUtil.getCon("studentdb");
String sql="delete from student_score where ID=? and Name=?";
PreparedStatement prep=con.prepareStatement(sql);
prep.setString(1, id);
prep.setString(2, name);
prep.executeUpdate();
System.out.println("学生成绩删除成功");
JOptionPane.showMessageDialog(null, "学生成绩删除成功!");
con.close();
}
}
Update 修改功能函数的实现
package Student_Manage_System;
import java.sql.Connection;
import java.sql.PreparedStatement;
import javax.swing.JOptionPane;
public class Update {
public static void Update_toinfo(String id,String name,String nsex,
String nage,String nstudydate,String nmajor,int nclass,String naddress,String ntel) throws Exception{
Connection con=DBUtil.getCon("studentdb");
String sql="update student_info set Sex=?,Age=?,Studydate=?,Major=?," +
"Class=?,Address=?,Tel=?,Name=? where ID=?";
PreparedStatement prep=con.prepareStatement(sql);
prep.setString(1, nsex);
prep.setString(2, nage);
prep.setString(3, nstudydate);
prep.setString(4, nmajor);
prep.setInt(5, nclass);
prep.setString(6, naddress);
prep.setString(7, ntel);
prep.setString(8,name);
prep.setString(9, id);
prep.executeUpdate();
System.out.println("学生信息修改成功");
JOptionPane.showMessageDialog(null, "学生信息修改成功");
con.close();
}
public static void Update_toscore(String id,String name,String nmajor,
int nclass,String nmath,String njava,String nmysql) throws Exception{
Connection con=DBUtil.getCon("studentdb");
String sql="update student_score set Major=?,Class=?,Math=?," +
"Java=?,MySQL=?,Name=? where ID=?";
PreparedStatement prep=con.prepareStatement(sql);
prep.setString(1, nmajor);
prep.setInt(2, nclass);
prep.setString(3, nmath);
prep.setString(4, njava);
prep.setString(5, nmysql);
prep.setString(6, name);
prep.setString(7, id);
prep.executeUpdate();
System.out.println("学生成绩修改成功");
JOptionPane.showMessageDialog(null, "学生成绩修改成功");
con.close();
}
}
Select 查找功能函数的实现
package Student_Manage_System;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;
public class Select {
static boolean is_select=false;
public void display1(ResultSet result) throws Exception
{
while(result.next()){
is_select=true;
JOptionPane.showMessageDialog(null, "学号:"+result.getInt("ID")+","+"姓名:"+result.getString("Name")+","+"性别:"+result.getString("Sex")+","
+"年龄:"+result.getInt("Age")+","+"入学日期:"+result.getString("Studydate")+","+"专业:"+result.getString("Major")+","
+"班级:"+result.getInt("Class")+","+"地址:"+result.getString("Address")+","+"联系方式:"+result.getString("Tel"));
System.out.println("学号:"+result.getInt("ID")+","+"姓名:"+result.getString("Name")+","+"性别:"+result.getString("Sex")+","
+"年龄:"+result.getInt("Age")+","+"入学日期:"+result.getString("Studydate")+","+"专业:"+result.getString("Major")+","
+"班级:"+result.getInt("Class")+","+"地址:"+result.getString("Address")+","+"联系方式:"+result.getString("Tel"));
}
}
public void display2(ResultSet result) throws Exception{
while(result.next()){
is_select=true;
JOptionPane.showMessageDialog(null, "学号:"+result.getInt("ID")+","+"姓名:"+result.getString("Name")+","+
"专业:"+result.getString("Major")+","+"班级:"+result.getInt("Class")+","+
"Math成绩:"+result.getString("Math")+","+"Java成绩:"+result.getString("Java")+","+
"MySQL成绩:"+result.getString("MySQL"));
System.out.println("学号:"+result.getInt("ID")+","+"姓名:"+result.getString("Name")+","+
"专业:"+result.getString("Major")+","+"班级:"+result.getInt("Class")+","+
"Math成绩:"+result.getString("Math")+","+"Java成绩:"+result.getString("Java")+","+
"MySQL成绩:"+result.getString("MySQL"));
}
}
public static boolean Select_info_ID(String id) throws Exception{
Connection con=DBUtil.getCon("studentdb");
String sql="select * from student_info where ID=? ";
PreparedStatement prep=con.prepareStatement(sql);
prep.setString(1, id);
ResultSet result=prep.executeQuery();
boolean is_select=false;
if(result.next()){
System.out.println("查找成功!");
is_select=true;
}
con.close();
return is_select;
}
public static boolean Select_score_ID(String id) throws Exception{
Connection con=DBUtil.getCon("studentdb");
String sql="select * from student_score where ID=? ";
PreparedStatement prep=con.prepareStatement(sql);
prep.setString(1, id);
ResultSet result=prep.executeQuery();
boolean is_select=false;
if(result.next()){
System.out.println("查找成功!");
is_select=true;
}
con.close();
return is_select;
}
public static void Select_info() throws Exception{
Connection con=DBUtil.getCon("studentdb");
String sql="select * from student_info ";
PreparedStatement prep=con.prepareStatement(sql);
ResultSet result=prep.executeQuery();
Select s=new Select();
System.out.print("全部学生信息查找成功!");
JOptionPane.showMessageDialog(null, "全部学生信息查找成功!");
s.display1(result);
con.close();
}
public static boolean Select_info(String id,String name) throws Exception{
Connection con=DBUtil.getCon("studentdb");
String sql="select * from student_info where ID=? and Name=?";
PreparedStatement prep=con.prepareStatement(sql);
prep.setString(1, id);
prep.setString(2, name);
ResultSet result=prep.executeQuery();
is_select=false;
Select s=new Select();
s.display1(result);
con.close();
return is_select;
}
public static boolean Select_major_info(String major) throws Exception{
Connection con=DBUtil.getCon("studentdb");
String sql="select * from student_info where Major=?";
PreparedStatement prep=con.prepareStatement(sql);
prep.setString(1, major);
ResultSet result=prep.executeQuery();
is_select=false;
Select s=new Select();
s.display1(result);
con.close();
return is_select;
}
public static boolean Select_class_info(String classid) throws Exception{
Connection con=DBUtil.getCon("studentdb");
String sql="select * from student_info where Class=?";
PreparedStatement prep=con.prepareStatement(sql);
prep.setString(1, classid);
ResultSet result=prep.executeQuery();
ResultSet result1=result;
Select s=new Select();
is_select=false;
s.display1(result);
con.close();
return is_select;
}
public static void Select_score() throws Exception{
Connection con=DBUtil.getCon("studentdb");
String sql="select * from student_score";
PreparedStatement prep=con.prepareStatement(sql);
ResultSet result=prep.executeQuery();
Select s=new Select();
System.out.print("全部学生成绩查找成功!");
JOptionPane.showMessageDialog(null, "全部学生成绩查找成功!");
s.display2(result);
con.close();
}
public static boolean Select_score(String id,String name) throws Exception{
Connection con=DBUtil.getCon("studentdb");
String sql="select * from student_score where ID=? and Name=?";
PreparedStatement prep=con.prepareStatement(sql);
prep.setString(1, id);
prep.setString(2, name);
ResultSet result=prep.executeQuery();
is_select=false;
Select s=new Select();
s.display2(result);
con.close();
return is_select;
}
public static boolean Select_major_score(String major) throws Exception{
Connection con=DBUtil.getCon("studentdb");
String sql="select * from student_score where Major=?";
PreparedStatement prep=con.prepareStatement(sql);
prep.setString(1, major);
ResultSet result=prep.executeQuery();
Select s=new Select();
is_select=false;
s.display2(result);
con.close();
return is_select;
}
public static boolean Select_class_score(String classid) throws Exception{
Connection con=DBUtil.getCon("studentdb");
String sql="select * from student_score where Class=?";
PreparedStatement prep=con.prepareStatement(sql);
prep.setString(1, classid);
ResultSet result=prep.executeQuery();
ResultSet result1=result;
Select s=new Select();
is_select=false;
s.display2(result);
con.close();
return is_select;
}
public static void Select_avg() throws Exception{
Connection con=DBUtil.getCon("studentdb");
String sql="select avg(Math),avg(Java),avg(MySQL) from student_score ";
PreparedStatement prep=con.prepareStatement(sql);
ResultSet result=prep.executeQuery();
System.out.println("查看各科成绩平均分:Math,Java,MySQL");
JOptionPane.showMessageDialog(null, "查看各科成绩平均分:Math,Java,MySQL");
while(result.next()){
JOptionPane.showMessageDialog(null, "Math平均分:"+result.getString("avg(Math)")+","+"Java平均分:"+result.getString("avg(Java)")+","+
"MySQL平均分:"+result.getString("avg(MySQL)"));
System.out.println("Math平均分:"+result.getString("avg(Math)")+","+"Java平均分:"+result.getString("avg(Java)")+","+
"MySQL平均分:"+result.getString("avg(MySQL)"));
}
con.close();
}
public static void Select_max() throws Exception{
Connection con=DBUtil.getCon("studentdb");
String sql="select max(Math),max(Java),max(MySQL) from student_score";
PreparedStatement prep=con.prepareStatement(sql);
ResultSet result=prep.executeQuery();
System.out.println("查看各科成绩最高分:Math,Java,MySQL");
JOptionPane.showMessageDialog(null,"查看各科成绩最高分:Math,Java,MySQL");
while(result.next()){
JOptionPane.showMessageDialog(null,"Math最高分:"+result.getString("max(Math)")+","+"Java最高分:"+result.getString("max(Java)")+","+"MySQL最高分:"+result.getString("max(MySQL)"));
System.out.println("Math最高分:"+result.getString("max(Math)")+","+"Java最高分:"+result.getString("max(Java)")+","+"MySQL最高分:"+result.getString("max(MySQL)"));
}
con.close();
}
public static void Select_min() throws Exception{
Connection con=DBUtil.getCon("studentdb");
String sql="select min(Math),min(Java),min(MySQL) from student_score";
PreparedStatement prep=con.prepareStatement(sql);
ResultSet result=prep.executeQuery();
System.out.println("查看各科成绩最低分:Math,Java,MySQL");
JOptionPane.showMessageDialog(null,"查看各科成绩最低分:Math,Java,MySQL");
while(result.next()){
JOptionPane.showMessageDialog(null,"Math最低分:"+result.getString("min(Math)")+","+"Java最低分:"+result.getString("min(Java)")+","+"MySQL最低分:"+result.getString("min(MySQL)"));
System.out.println("Math最低分:"+result.getString("min(Math)")+","+"Java最低分:"+result.getString("min(Java)")+","+"MySQL最低分:"+result.getString("min(MySQL)"));
}
con.close();
}
}