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.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 {
JFrame jf = new JFrame("成绩录入界面");
jf.setLayout(new GridLayout(7, 2));
JLabel a[] = new JLabel[6];
JTextField b[] = new JTextField[6];
JButton c[] = new JButton[6];
for (int i = 0; i < 6; 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("开课专业");
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) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/new_schema?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 five_table 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);
new myjd1();
} catch (SQLException e1) {
e1.printStackTrace();
} catch (ClassNotFoundException e1) {
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.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);
}
}