📄 studentdataadd.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.sql.*;
class StudentDataAdd extends JFrame implements ActionListener{
String title[]={"学号","姓名","性别","年龄","专业","系名","学院名称"};
//JComboBox combClassID=new JComboBox();
JTextField txtNo=new JTextField(10);
JTextField txtName=new JTextField(10);
JTextField txtAge=new JTextField(10);
JTextField txtZhuanYe=new JTextField(20);
JTextField txtXiName=new JTextField(20);
JTextField txtXueYuan=new JTextField(20);
JRadioButton radioSexM=new JRadioButton("男",true);
JRadioButton radioSexF=new JRadioButton("女",false);
JButton ok=new JButton("保存");
JButton cancel=new JButton("取消");
Statement stmt;
ResultSet rs;
String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=dbstu";
Connection con;
//int No;
StudentDataAdd(){
//this.No=No;
// if(No==-1)
setTitle("添加学生窗口");
//else setTitle("修改学生档案窗口");
try{
Container con=getContentPane();
con.setLayout(new BorderLayout(0,5)); //设置边界布局
// stmt = con.createStatement(); //stmt=ConnectServer2.con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); //建立可滚动并且可更新的结果集
// rs=stmt.executeQuery("select * from student");
//while(rs.next()){ //将班级信息添入到下拉列表中 order by 学号"
// combClassID.addItem(rs.getString(1));}
ButtonGroup bgp=new ButtonGroup(); //为单选钮分组
bgp.add(radioSexM);bgp.add(radioSexF);
setSize(450,410);
JPanel p[]=new JPanel[7];
for(int i=0;i<7;i++){
p[i]=new JPanel(new FlowLayout(FlowLayout.LEFT,10,0));
p[i].add(new JLabel(title[i]));
}
// p[0].add(combClassID);
p[0].add(txtNo);
p[1].add(txtName);
p[2].add(radioSexM);p[2].add(radioSexF);
p[3].add(txtAge);
p[4].add(txtZhuanYe);
p[5].add(txtXiName);
p[6].add(txtXueYuan);
JPanel p1=new JPanel(new GridLayout(7,1,0,5));
for(int i=0;i<7;i++) p1.add(p[i]);
// JPanel p2=new JPanel(new FlowLayout(FlowLayout.LEFT,10,0));
// JScrollPane jp=new JScrollPane(txtResume,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
// jp.setPreferredSize(new Dimension(370,80));
// p2.add(new JLabel(title[7]));p2.add(jp);con.add(p2,"Center");
JPanel p3=new JPanel();
p3.add(ok);p3.add(cancel);
JPanel p2=new JPanel();
con.add(p2,"North"); con.add(p1,"Center");con.add(p3,"South");
ok.addActionListener(this);
cancel.addActionListener(this);
//if(No!=-1)
/*{
rs=stmt.executeQuery("select student.no,student.name,student.sex,student.age,student.zhuye,speciality.specialityname,xsda.address,xsda.resume from xsda,speciality,classclass where xsda.classID=classclass.ID and xsda.speciality=speciality.specialityID and xsda.no=");//+No
if(rs.next())
{rs.first();
loadData();
txtNo.setEditable(false);
}
else
JOptionPane.showMessageDialog(null,"没有找到相应的记录!");
}*/
rs.close();
}catch(Exception e){e.printStackTrace();}
setVisible(true);
}
boolean loadData(){ //将数据填到组件
try{
//combClassID.setSelectedItem(rs.getString(1));
txtNo.setText(rs.getString(1));
txtName.setText(rs.getString(2));
if(rs.getString(3).equals("男"))
radioSexM.setSelected(true);
else
radioSexF.setSelected(true);
txtAge.setText(rs.getString(4));
/*if(rs.getString(6).toUpperCase().equals("Y"))
checkIsMember.setSelected(true);
else
checkIsMember.setSelected(false);*/
txtZhuanYe.setText(rs.getString(5));
txtXiName.setText(rs.getString(6));
txtXueYuan.setText(rs.getString(7));
}catch(SQLException e){e.printStackTrace();return false;}
return true;
}
public void actionPerformed(ActionEvent e){
try{
if(e.getSource()==ok){
String sex;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch(java.lang.ClassNotFoundException eaa) {
System.err.print("ClassNotFoundException: ");
System.err.println(eaa.getMessage());
}
con = DriverManager.getConnection("jdbc:odbc:dbstu","","");
//int classID;
Statement stmt = con.createStatement();//stmt=ConnectServer2.con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rs=stmt.executeQuery("select * from student");//name='"+combClassID.getSelectedItem()+"'"
rs.first();
//txtNo=rs.getSting(1);
if(radioSexM.isSelected()) sex="男";
else sex="女";
// if(checkIsMember.isSelected()) isMember="Y";
// else isMember="N";
String sql;
// if(No==-1)
{ //添加记录Integer.parseInt()
sql="insert into student(学号,姓名,性别,年龄,专业,系名,学院名称)";
sql=sql+" values("+txtNo.getText()+",'" +txtName.getText()+"','"+sex;
sql=sql+"','"+txtAge.getText()+"','"+txtZhuanYe.getText()+"','";
sql=sql+txtXiName.getText()+"','"+txtXueYuan.getText()+"')";
System.out.println(sql);
}
/* else{ //修改记录
sql="update xsda set classID="+classID;
sql=sql+",Name='"+txtName.getText();
sql=sql+"',sex='"+sex+"',Age='"+txtAge.getText();
sql=sql+"',isMember='"+isMember;
sql=sql+"',address='"+txtAddress.getText();
sql=sql+"',resume='"+txtResume.getText()+"' where no="+No;
}*/
stmt.executeUpdate(sql); //执行SQL语句
}
else if(e.getSource()==cancel) dispose();
}catch(SQLException ee){JOptionPane.showMessageDialog(this,""+ee);}
}
/* public static void main(String args[]){
JFrame.setDefaultLookAndFeelDecorated(true);
Font font = new Font("JFrame", Font.PLAIN, 14);
Enumeration keys = UIManager.getLookAndFeelDefaults().keys();
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
if(UIManager.get(key) instanceof Font)UIManager.put(key, font);
}
if(!ConnectServer2.conn("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=dbstu","sa","")){
JOptionPane.showMessageDialog(null,"数据库连接不成功!");
System.exit(0);
}
new StudentDataAdd(); //-1添加新记录的调用方法
// new StudentDataUpdate(2); //修改学号为2的记录
}*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -