java+swing+mysql
这是大二的java课设,主要是用了 Eclipse,windowbuilder插件和mysql完成,介于csdn上大多数界面做的很复古,让人以为swing做的界面很丑,所以我发布这个博客反驳一下,swing照样可以做的很好看(可能我做的还不是很好看)。
windowbuilder
windowbuider是个挺好的插件,但是有人说他并不好,其一 容易崩溃,其二 写的代码太乱。我通过摸索得到了更多的windowbuilder正确使用的方法,但是windowbuilder的使用教程并不在本博客内。
先上主界面图片
这是今年做的管理系统主界面qu图片:
插入和查询等功能都实现了 查询功能还实现了不同科目的排名
右边的滚动条还是有点丑,实在不会操作了,不过比原版的JTable好看多了
fng一下去年做的被老师吐槽的界面:
是不是差距瞬间就有了,看了今年的这个界面,去年的简直不忍直视
#层次结构
当然我现在其实还是初学者- -结构还是很乱的
代码
//student
public class student {
private String name;
private double java;
private double math;
private double datastruct;
private double english;
private double total;
private double Average;
private int paiming;
public int getPaiming() {
return paiming;
}
public void setPaiming(int paiming) {
this.paiming = paiming;
}
public student(String name, double java, double math, double datastruct, double english, double total,
double average) {
super();
this.name = name;
this.java = java;
this.math = math;
this.datastruct = datastruct;
this.english = english;
this.total = total;
Average = average;
}
@Override
public String toString() {
return "student [name=" + name + ", java=" + java + ", math=" + math + ", datastruct=" + datastruct
+ ", english=" + english + ", total=" + total + ", Average=" + Average + "]";
}
public student() {
// TODO Auto-generated constructor stub
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getJava() {
return java;
}
public void setJava(double java) {
this.java = java;
}
public double getMath() {
return math;
}
public void setMath(double math) {
this.math = math;
}
public double getDatastruct() {
return datastruct;
}
public void setDatastruct(double datastruct) {
this.datastruct = datastruct;
}
public double getEnglish() {
return english;
}
public void setEnglish(double english) {
this.english = english;
}
public double getTotal() {
return total;
}
public void setTotal(double total) {
this.total = total;
}
public double getAverage() {
return Average;
}
public void setAverage(double average) {
Average = average;
}
}
//dao
import java.sql.SQLException;
import java.util.List;
import beans.student;
public interface dao {
public abstract int add(String name,double java,double math,double datastruct,double english);
//添加
public abstract int remove(String name) throws SQLException, ClassNotFoundException;
//删除通过姓名
public abstract int removexuehao(int xuhao) throws SQLException, ClassNotFoundException;
//删除通过序号
//修改
public abstract student selectName(String name);
//根据姓名查询
public abstract List<student> sortjava() throws SQLException;
//排序
public abstract List<student> sortmath() throws SQLException;
public abstract List<student> sortdatastruct() throws SQLException;
public abstract List<student> sortenglish() throws SQLException;
public abstract List<student> sortzongfen() throws SQLException;
public abstract List<student> sortpingjunfen() throws SQLException;
public abstract List<student> show();
}
//显示所有联系人
//daoimp
public class daoimp implements dao {
private Connection conn;
private PreparedStatement pst;
private ResultSet res;
public int add(String name,double java,double math,double datastruct,double english) {
int a = 0;
double total =java+math+datastruct+english;
double average = (java+math+datastruct+english)/4;
System.out.println(average);
try {
conn = (Connection) dbutils.getConn();
String sql = "insert into student(name,java,math,datastruct,english,zongfen,pingjunfen) values (?,?,?,?,?,?,?)";
pst=conn.prepareStatement(sql); // 预编译sql语句
//repareStatement这个方法会将SQL语句加载到驱动程序conn集成程序中
//但是并不直接执行,而是当它调用execute()方法的时候才真正执行
pst.setString(1,name); //给第1个占位符赋值
pst.setDouble(2,java);
pst.setDouble(3,math);
pst.setDouble(4,datastruct);
pst.setDouble(5,english);
pst.setDouble(6,total);
pst.setDouble(7,average);
a=pst.executeUpdate(); // 执行SQL语句
System.out.println("hasjaj");
} catch (SQLException e) {
} catch (ClassNotFoundException e) {
}finally {
dbutils.close(pst,conn,res);
}
return a;
}
@Override
public int remove(String name) {
int a = 0;
try {
conn = (Connection) dbutils.getConn();
String sql = "delete from student where name=?";
pst=conn.prepareStatement(sql);// 预编译sql语句
pst.setString(1,name);
a = pst.executeUpdate();// 执行SQL语句
} catch (SQLException e) {
} catch (ClassNotFoundException e) {
}finally {
dbutils.close(pst,conn,res);
}
return a;
}
@Override
public student selectName(String name) {
student person = null;
try{
conn = (Connection) dbutils.getConn();
String sql = "select * from student where name = ?";
pst=conn.prepareStatement(sql);// 预编译sql语句
pst.setString(1,name);
res = pst.executeQuery();// 执行SQL语句
if(res.next()) {
person = new student();
person.setName(res.getString("name"));
person.setJava(res.getDouble("java"));
person.setMath(res.getDouble("math"));
person.setDatastruct(res.getDouble("datastruct"));
person.setEnglish(res.getDouble("english"));
person.setTotal(res.getDouble("zongfen"));
person.setAverage(res.getDouble("pingjunfen"));
}
}catch (SQLException e){
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (Throwable e) {
e.printStackTrace();
}finally {
dbutils.close(pst,conn,res);
return person;
}
}
/* @Override
public student selectNum(String num) {
student person = null;
try{
conn = (Connection) dbutils.getConn();
String sql = "select * from tongxunlu where num = ?";
pst=conn.prepareStatement(sql);// 预编译sql语句
pst.setString(1,num);
res = pst.executeQuery();// 执行SQL语句
if(res.next()) {
person = new student();
person.setName(res.getString("name"));
person.setJava(res.getDouble("ss"));
person.setMath(res.getDouble("num"));
person.setDatastruct(res.getDouble("school"));
person.setTotal(res.getDouble("e_mail"));
person.setAverage(res.getDouble("guanxi"));
}
}catch (SQLException e){
} catch (ClassNotFoundException e) {
}finally {
dbutils.close(pst,conn,res);
return person;
}
}
*/
@Override
public List<student> sortjava() {
List<student> list = new ArrayList<>();
try {
conn = (Connection) dbutils.getConn();
String sql = "select * From student ORDER BY java desc";
pst = conn.prepareStatement(sql);
res = pst.executeQuery();
while(res.next()){
student person = new student();
person.setPaiming(res.getInt("paiming"));
person.setName(res.getString("name"));
person.setJava(res.getDouble("java"));
person.setMath(res.getDouble("math"));
person.setDatastruct(res.getDouble("datastruct"));
person.setEnglish(res.getDouble("english"));
person.setTotal(res.getDouble("zongfen"));
person.setAverage(res.getDouble("pingjunfen"));
list.add(person);
}
for(student c:list){
System.out.println(c.toString());
}
} catch (SQLException e) {
} catch (ClassNotFoundException e) {
}finally {
dbutils.close(pst,conn,res);
}
return list;
}
@SuppressWarnings("finally")
@Override
public List<student> show() {
List<student> list = new ArrayList<>();
try {
conn = (Connection) dbutils.getConn();
String sql = "select * from student ORDER BY zongfen desc";
pst=conn.prepareStatement(sql);
res = pst.executeQuery();
while(res.next()){
student person = new student();
person.setName(res.getString("name"));
person.setJava(res.getDouble("java"));
person.setMath(res.getDouble("math"));
person.setDatastruct(res.getDouble("datastruct"));
person.setDatastruct(res.getDouble("english"));
person.setTotal(res.getDouble("zongfen"));
person.setAverage(res.getDouble("pingjunfen"));
list.add(person);
}
for(student c:list){
System.out.println(c.toString());
}
} catch (SQLException e) {
} catch (ClassNotFoundException e) {
}finally {
dbutils.close(pst,conn,res);
return list;
}
}
public int removeall() {
int a = 0;
try {
conn = (Connection) dbutils.getConn();
String sql = "truncate table student";
pst=conn.prepareStatement(sql);// 预编译sql语句
a = pst.executeUpdate();// 执行SQL语句
} catch (SQLException e) {
} catch (ClassNotFoundException e) {
}finally {
dbutils.close(pst,conn,res);
}
return a;
}
public void chongxinbaocun(List<student> l) {
int a = 0;
for(student c:l){
try {
conn = (Connection) dbutils.getConn();
conn = (Connection) dbutils.getConn();
String sql = "insert into student(name,java,math,datastruct,english,zongfen,pingjunfen) values (?,?,?,?,?,?,?)";
pst=conn.prepareStatement(sql); // 预编译sql语句
//repareStatement这个方法会将SQL语句加载到驱动程序conn集成程序中
//但是并不直接执行,而是当它调用execute()方法的时候才真正执行
pst.setString(1,c.getName()); //给第1个占位符赋值
pst.setDouble(2,c.getJava());
pst.setDouble(3,c.getMath());
pst.setDouble(4,c.getDatastruct());
pst.setDouble(5,c.getEnglish());
pst.setDouble(6,c.getTotal());
pst.setDouble(7,c.getAverage());
a=pst.executeUpdate(); // 执行SQL语句 }
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
dbutils.close(pst,conn,res);
}
}
}
public static void main(String[] args) {
daoimp a = new daoimp();
List<student> b = a.show();
a.removeall();
a.chongxinbaocun(b);
}
@Override
public int removexuehao(int xuhao) throws SQLException, ClassNotFoundException {
int a = 0;
try {
conn = (Connection) dbutils.getConn();
String sql = "delete from student where xuehao=?";
pst=conn.prepareStatement(sql);// 预编译sql语句
pst.setInt(1,xuhao);
a = pst.executeUpdate();// 执行SQL语句
} catch (SQLException e) {
} catch (ClassNotFoundException e) {
}finally {
dbutils.close(pst,conn,res);
}
return a;
}
@Override
public List<student> sortmath() throws SQLException {
List<student> list = new ArrayList<>();
try {
conn = (Connection) dbutils.getConn();
String sql = "select * From student ORDER BY math desc";
pst = conn.prepareStatement(sql);
res = pst.executeQuery();
while(res.next()){
student person = new student();
person.setPaiming(res.getInt("paiming"));
person.setName(res.getString("name"));
person.setJava(res.getDouble("java"));
person.setMath(res.getDouble("math"));
person.setDatastruct(res.getDouble("datastruct"));
person.setEnglish(res.getDouble("english"));