package Nobody_care;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Connection;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
public class Mysql_test {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
// TODO Auto-generated method stub
JFrame jf = new JFrame("成绩录入界面");
jf.setLayout(new GridLayout(8, 2));
JLabel a[] = new JLabel[8];
JTextField b[] = new JTextField[8];
JButton c[] = new JButton[8];
for (int i = 0; i < 8; i++) {
a[i] = new JLabel();
b[i] = new JTextField();
c[i] = new JButton();
}
a[0].setText("学号");
a[1].setText("姓名");
a[2].setText("课程名称");
a[3].setText("课程学分");
a[4].setText("课程成绩");
a[5].setText("开课专业");
a[6].setText("数据库记录数:");
a[7].setText("");
c[0].setText("提交");
c[1].setText("重置");
c[2].setText("查询");
c[1].addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < 6; i++) {
b[i].setText("");
}
}
});
c[0].addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
// // TODO Auto-generated method stub
// String xf = "[1-9]";
// String xm = "^[\\u4e00-\\u9fa5_a-zA-Z_]{1,10}";
// String cj = "^(100|(([1-9]){1}[0-9]?|0{1})((\\.)([0-9]){1,2})?)$";
// if (!b[0].getText().matches(xm)) {
// b[0].setText("");
// }
// if (!b[1].getText().matches(xf)) {
// b[1].setText("");
// }
// if (!b[2].getText().matches(cj)) {
// b[2].setText("");
// }
// if (!b[3].getText().matches(xm)) {
// b[3].setText("");
// }
// if (b[0].getText().matches(xm) && b[1].getText().matches(xf) && b[2].getText().matches(cj)
// && b[3].getText().matches(xm)) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/student?useSSL=false&serverTimezone=UTC";
String name = "root";
String password = "123456";
Connection d= DriverManager.getConnection(url,name,password);
double gpa=get(Double.parseDouble(b[4].getText()));
String in1="insert into students values(";
in1+=b[0].getText()+',' +'\''+b[1].getText()+'\''+',' +'\''+b[2].getText()+'\''+',' +b[3].getText()+',' +b[4].getText()+','
+'\''+b[5].getText()+'\''+','+gpa+")";
Statement st=d.createStatement();
st.executeUpdate(in1);
ResultSet i=st.executeQuery("select *from students");
int n=0;
while(i.next()) n++;
a[7].setText(n+"");
new myjd1();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
for(int i=0;i<6;i++) {
jf.add(a[i]);jf.add(b[i]);
}
jf.add(c[0]);jf.add(c[1]);
jf.add(a[6]);jf.add(a[7]);
jf.setBounds(500, 250, 400, 400);
jf.setVisible(true);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static double get(Double n) {
Double j = 0.0;
if (n >= 90) {
j = 4.0;
} else if (n >= 85) {
j = 3.5;
} else if (n >= 80) {
j = 3.0;
} else if (n >= 75) {
j = 2.5;
} else if (n >= 70) {
j = 2.0;
} else if (n >= 65) {
j = 1.5;
} else if (n >= 60) {
j = 1.0;
}
return j;
}
}
class myjd1 extends JDialog {
public myjd1() {
this.setVisible(true);
this.setBounds(500, 250, 200, 80);
Container c = this.getContentPane();
JLabel l1 = new JLabel("提交成功", JLabel.CENTER);
c.add(l1);
}
}
中国地质大学北京信息管理与信息系统——Java程序设计第四次上机part1(改进,显示条数)
最新推荐文章于 2026-01-08 20:20:31 发布
该博客展示了如何使用Java Swing创建一个GUI界面,用于输入学生成绩并将其存储到MySQL数据库中。程序包含字段验证,数据录入,以及查询功能。用户可以提交、重置和查询成绩,提交后会显示数据库中的记录总数。
357

被折叠的 条评论
为什么被折叠?



