中国地质大学北京信息管理与信息系统——Java程序设计第四次上机part0(老天爷,终于写出来了)

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 {

		// TODO Auto-generated method stub
		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) {
//				// 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/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) {
						// 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.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);
	}
}

记住表里的元素要和我的一样

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值