【Java开源代码栏目提醒】:网学会员--在 Java开源代码编辑为广大网友搜集整理了:AddFI.java绩等信息,祝愿广大网友取得需要的信息,参考学习。
package sfms;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
public class AddFI extends JFrame implements ActionListener{
//variables needing class scope
JTextField STNOText,SNameText,genderText,departmentText,addressText,telText;
JButton addButton,clearButton,closeButton;
FileInformation student;
String STNO,SName,Gender,Department,Address,Tel;
mainWindow parentWindow;
//constructor
public AddFI(mainWindow main){
parentWindow=main;
Container c=this.getContentPane();
c.setLayout(new GridLayout(3,1));
JPanel centerPanel=new JPanel(new GridLayout(6,2));
JPanel lowerPanel=new JPanel(new FlowLayout());
//create logo
JLabel logoLabel=new JLabel("",SwingConstants.CENTER);
logoLabel.setForeground(Color.blue);
logoLabel.setFont(new Font("TRUE",Font.TRUETYPE_FONT,20));
logoLabel.setText("学生档案信息");
c.add(logoLabel);
//create TextField for FileInformation
STNOText=new JTextField();
SNameText=new JTextField();
genderText=new JTextField();
departmentText=new JTextField();
addressText=new JTextField();
telText=new JTextField();
//add labels and textfields to the panels
centerPanel.add(new JLabel("",SwingConstants.CENTER));
centerPanel.add(STNOText);
centerPanel.add(new JLabel("",SwingConstants.CENTER));
centerPanel.add(SNameText);
centerPanel.add(new JLabel("性别",SwingConstants.CENTER));
centerPanel.add(genderText);
centerPanel.add(new JLabel("系别",SwingConstants.CENTER));
centerPanel.add(departmentText);
centerPanel.add(new JLabel("籍贯",SwingConstants.CENTER));
centerPanel.add(addressText);
centerPanel.add(new JLabel("电话",SwingConstants.CENTER));
centerPanel.add(telText);
c.add(centerPanel) ;
//create & add Buttons to bottom panel
addButton=new JButton("添加");
clearButton=new JButton("清除");
closeButton=new JButton("退出");
lowerPanel.add(addButton);
lowerPanel.add(clearButton);
lowerPanel.add(closeButton);
c.add(lowerPanel);
//register frames as listener for button events
addButton.addActionListener(this);
clearButton.addActionListener(this);
closeButton.addActionListener(this);
this.setSize(400,300);
this.setTitle("添加档案");
this.setVisible(true);
//create anonymous inner class to handle window closing event
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent event){
System.exit(0);
}
});
}
//actionPerformed is involked when a Button is clicked
public void actionPerformed(ActionEvent e){
//see which button was clicked
if(e.getSource()==addButton)
{addFI();}
if(e.getSource() ==clearButton)
{clearForm();}
if(e.getSource() ==closeButton)
{shutDown();}
}
private void addFI(){
STNO=STNOText.getText();
SName=SNameText.getText();
Gender=genderText.getText();
Department=departmentText.getText();
Address=addressText.getText();
Tel=telText.getText();
if(STNO.length()==0||SName.length()==0||Gender.length()==0||Department.length()==0||Address.length()==0||Tel.length()==0)
JOptionPane.showMessageDialog(this,"请添加完全信息");
else
{
try{
Class.forName("oracle.jdbc.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@localhost:1521:SFMS";
Connection conn=DriverManager.getConnection(url,"df","1234");
String
sql="INSERT INTO DF.FILEINFO VALUES(?,?,?,?,?,?)";
PreparedStatement ps=conn.prepareStatement(sql);
ps.setString(1,STNO);
ps.setString(2,SName);
ps.setString(3,Gender);
ps.setString(4,Department);
ps.setString(5,Address);
ps.setString(6,Tel);
ps.executeUpdate();
conn.close();
JOptionPane.showMessageDialog(this,"添加成功!");
clearForm();
}
catch(SQLException e){
System.out.println("E Code"+ e.getErrorCode());
System.out.println("E M="+e.getMessage());
}
catch(Exception e){
e.printStackTrace();
}
}
}
private void clearForm(){
STNOText.setText("");
SNameText.setText("");
genderText.setText("");
departmentText.setText("");
addressText.setText("");
telText.setText("");
STNOText.requestFocus();
}
private void shutDown(){
parentWindow.setVisible(true);
this.dispose();
}
/* public static void main(String[] args){
AddFI frame1=new AddFI();
frame1.show();
}
*/
}