【Java开源代码栏目提醒】:本文主要为网学会员提供AddStudent.java,希望对需要AddStudent.java网友有所帮助,学习一下!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import javax.swing.event.*;
import java.sql.*;
class AddStudent extends JFrame
implements ActionListener
{
JLabel JL =new JLabel("添 加 基 本 信 息",JLabel.CENTER);
JLabel JLNumber=new JLabel(":");
JTextField JTNumber=new JTextField();
JLabel JLName =new JLabel(":");
JTextField JTName =new JTextField();
JLabel JLClass =new JLabel("班级:");
JTextField JTClass =new JTextField();
JLabel JLsex =new JLabel("性别:");
ButtonGroup BG =new ButtonGroup();
JRadioButton JRB1 =new JRadioButton("男");
JRadioButton JRB2 =new JRadioButton("女");
JLabel JL1 =new JLabel(":");
JTextField JT1 =new JTextField();
JLabel JL2 =new JLabel("生日:");
JTextField JT2 =new JTextField();
JButton JBAdd =new JButton("添加");
JButton JBNext =new JButton("重置");
JButton JBExit =new JButton("退出");
String sql="";
public AddStudent()
{
this.setTitle("添加学生信息");
this.setLayout(null);
JL.setFont(new Font("TimesRoman",Font.BOLD,24));
JL.setForeground(Color.red);
JL.setFont(new java.awt.Font("宋体",Font.PLAIN,19));
JL.setBounds(100,30,200,40);
this.add(JL);
JLNumber.setBounds(100,80,100,20);
this.add(JLNumber);
JTNumber.setBounds(200,80,80,20);
this.add(JTNumber);
JLName.setBounds(100,120,60,20);
this.add(JLName);
JTName.setBounds(200,120,80,20);
this.add(JTName);
JLsex.setBounds(100,160,100,20);
this.add(JLsex);
JRB1.setBounds(200,160,40,20);
JRB2.setBounds(300,160,40,20);
this.add(JRB1);
this.add(JRB2);
BG.add(JRB1);
BG.add(JRB2);
JL2.setBounds(100,200,80,20);
this.add(JL2);
JT2.setBounds(200,200,80,20);
this.add(JT2);
JLClass.setBounds(100,240,60,20);
this.add(JLClass);
JTClass.setBounds(200,240,80,20);
this.add(JTClass);
JL1.setBounds(100,280,60,20);
this.add(JL1);
JT1.setBounds(200,280,80,20);
this.add(JT1);
JBAdd.setBounds(80,320,90,20);
this.add(JBAdd);
JBAdd.addActionListener(this);
JBNext.setBounds(190,320,90,20);
this.add(JBNext);
JBNext.addActionListener(this);
JBExit.setBounds(300,320,90,20);
this.add(JBExit);
JBExit.addActionListener(this);
this.setBounds(10,10,500,400);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==JBAdd)
{
System.out.println("ffffffffffffffffffff");
String snumber=JTNumber.getText();
String sname=JTName.getText();
String sclass=JTClass.getText();
String ssex="女";
if(JRB1.isSelected())
ssex="男";
String scollect=JT1.getText();
String sbir=JT2.getText();
sql="select * from student where Id='"+snumber+"'";
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection cot=DriverManager.getConnection("jdbc:odbc:student1","","");
Statement stm=cot.createStatement();
ResultSet rs=stm.executeQuery(
sql);
if(rs.next())
JOptionPane.showMessageDialog(null,"该号已经存在!");
else
{
sql="insert into student values('"+snumber+"','"+sname+"','"+sclass+"','"+ssex+"','"+scollect+"','"+sbir+"')";
int i=stm.executeUpdate(sql);
if(i>0)
JOptionPane.showMessageDialog(null,"添加成功!");
else
JOptionPane.showMessageDialog(null,"删除失败!");
}
}catch(Exception ee)
{
}
}
if(e.getSource()==JBNext)
{
JTNumber.setText(null);
JTName.setText(null);
JTClass.setText(null);
JT1.setText(null);
JT2.setText(null);
}
if(e.getSource()==JBExit)
{
setVisible(false);
}
}
public static void main(String args[])
{
new AddStudent();
}
}