日期:2014-05-20 浏览次数:20928 次
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.sql.*; public class Xg extends JFrame{ JTextField jtf1,jtf2,jtf3,jtf4,jtf0; JLabel jl1,jl2,jl3,jl4,jl0; JButton jb1; static Statement st; static { try { Class.forName("com.mysql.jdbc.Driver"); String url="jdbc:mysql://localhost:3306/test?"+"user=root&password=root"; Connection conn=DriverManager.getConnection(url); st=conn.createStatement(); } catch(Exception e){} } public Xg(){ JButton jb1=new JButton("提交"); jtf0=new JTextField(15); jtf0.setText("序列号为修改依据"); jtf1=new JTextField(15); jtf2=new JTextField(15); jtf3=new JTextField(15); jtf4=new JTextField(15); jl3=new JLabel("学号"); jl1=new JLabel("姓名"); jl2=new JLabel("身份"); jl4=new JLabel("成绩"); jl0=new JLabel("序列号"); JPanel jp1=new JPanel(); jp1.setLayout(new GridLayout(5,2)); jb1.addActionListener(new Mysql()); jp1.add(jl0); jp1.add(jtf0); jp1.add(jl1); jp1.add(jtf1); jp1.add(jl2); jp1.add(jtf2); jp1.add(jl3); jp1.add(jtf3); jp1.add(jl4); jp1.add(jtf4); jp1.add(jb1); Container container=getContentPane(); container.add(jp1,BorderLayout.CENTER ); container.add(jb1, BorderLayout.SOUTH); setTitle("更新数据窗口"); setSize(350,200); setVisible(true); } public class Mysql implements ActionListener{ public void actionPerformed(ActionEvent e){ System.out.println("-------------"); try { String str1=jtf1.getText().trim(); String str2=jtf2.getText().trim(); String str3=jtf3.getText().trim(); String str4=jtf4.getText().trim(); String str0=jtf0.getText().trim(); if(str1.equals("")|str2.equals("")|str3.equals("")|str0.equals("")){ JOptionPane.showMessageDialog(Xg.this, "请重新输入学生信息","提示对话框",1); } else { int n=Integer.parseInt(str0); //假如成绩这列是int类型,如果不是就会报错 int chengji=Integer.parseInt(str4); String sql="update student2 set v_name="+str1+",id="+str2+",xuehao="+str3+ ",chengji="+chengji+" where id="+n+""; st.executeUpdate(sql); System.out.println("执行SQL语句"); JOptionPane.showMessageDialog(Xg.this, "数据修改成功","提示对话框",1); jtf0.setText("");jtf1.setText("");jtf2.setText("");jtf3.setText(""); } } catch(Exception ee){} } } public static void main(String []args){ new Xg(); } }