import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.border.*;
import java.util.*;
public class Allmain{
//---------------------------------------------------------变量定义
//swing
JLabel conLabel,addLabel,modLabel,droLabel,aboLabel;
JButton conButton,addButton,modButton,droButton;
JTextField conField;
JFrame mainFrame;
//string
static final String JDBC="sun.jdbc.odbc.JdbcOdbcDriver";
static final String DATEBASE="jdbc:odbc:studyDSN";
//数据库API变量
public Connection connection;
public Statement statement;
String sqlString;
//----------------------------------------------------------------------
Allmain(){
mainFrame=new JFrame("数据库课程设计");
JPanel mainPanel=new JPanel();
mainPanel.setBackground(new Color(255,192,192));
mainPanel.setLayout(new GridLayout(5,2,5,5));
//------------------------------------------------------外观值
aboLabel=new JLabel("");
conLabel=new JLabel("数据库名称");
addLabel=new JLabel("添加数据");
modLabel=new JLabel("修改数据");
droLabel=new JLabel("删除数据");
conButton=new JButton("连接");
conButton.setBackground(new Color(50,192,192));
conButton.setFont(new Font("SansSerif",Font.PLAIN,18));
addButton=new JButton("添加");
addButton.setBackground(new Color(50,192,192));
addButton.setFont(new Font("SansSerif",Font.PLAIN,18));
modButton=new JButton("修改");
modButton.setBackground(new Color(50,192,192));
modButton.setFont(new Font("SansSerif",Font.PLAIN,18));
droButton=new JButton("删除");
droButton.setBackground(new Color(50,192,192));
droButton.setFont(new Font("SansSerif",Font.PLAIN,18));
conField=new JTextField(10);
//_______________________________________________________________________
mainPanel.add(conLabel);
mainPanel.add(conField);
mainPanel.add(aboLabel);
mainPanel.add(conButton);
mainPanel.add(addLabel);
mainPanel.add(addButton);
mainPanel.add(modLabel);
mainPanel.add(modButton);
mainPanel.add(droLabel);
mainPanel.add(droButton);
mainFrame.getContentPane().add(mainPanel);
mainFrame.setBounds(200,200,200,200);
mainFrame.setVisible(true);
//------------------------------------------------------------------添加进监听
conButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
con(); }});
addButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
add(); }});
modButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
mod(); }});
droButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
dro(); }});
//____________________________________________________________________________
}
//--------------------------------------------------------------------方法
void con(){
initize(); }
void add(){//添加
new AddStudentFrame();
}
void mod(){//修改
}
void dro(){//删除
}
void initize(){//初始化
try{
Class.forName(JDBC);
connection=DriverManager.getConnection(DATEBASE,"sa","");//注意:可改变
statement=connection.createStatement();
}
catch(SQLException sqlException){
JOptionPane.showMessageDialog(null,sqlException.getMessage(),"Database error",JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
catch(ClassNotFoundException classNotFound){
JOptionPane.showMessageDialog(null,classNotFound.getMessage(),"driver not found",JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
catch(Exception e){
System.out.print(e.toString());
}
}
//_____________________________________________________________________________
//----------------------------------------------------------------------------------------main
public static void main(String args[]){
new Allmain();
}
//___________________________________________________________________________________________________
//-------------------------------------------------------------------------------------AddStudentFrame
class AddStudentFrame extends JFrame{
private StudentUI userInterface;
private JButton clearButton,writeButton;
String names[]={"学号","姓名","性别","年龄","所在系"
};
public AddStudentFrame(){
super("添加一个学生记录");
userInterface=new StudentUI(names);
getContentPane().add(userInterface,BorderLayout.CENTER);
//--------------------------------------------------------------------------------保存也就是添加
writeButton=userInterface.getDotask1Button();
writeButton.setText("保存");
writeButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
addRecord();
}
});
//______________________________________________________________________________________________
//------------------------------------------------------------------------------------清除
clearButton=userInterface.getDoTask2Button();
clearButton.setText("清除");
clearButton.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
userInterface.clearFields();
}
});
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent event)
{
terminate();
}
});
//____________________________________________________________________________________________
setSize(300,200);
setVisible(true);
}
public void terminate(){
try{
statement.close();
connection.close();
}
catch(SQLException sqlException){
JOptionPane.showMessageDialog(null,sqlException.getMessage(),"database error",JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
}
//----------------------------------------------------------------------------方法
//addRecord()
public void addRecord(){
String fieldValues[]=userInterface.getFieldValues();
if(!fieldValues[StudentUI.SNO].equals("")){
try{
int numberAge=Integer.parseInt(fieldValues[StudentUI.SAGE]);
String sqlInsert="insert into student"+"values('"+fieldValues[0]+"','"+fieldValues[1]+"','"+fieldValues[2]+"',"+numberAge+",'"+fieldValues[4]+"')";
int result=statement.executeUpdate(sqlInsert);
if(result!=0){
userInterface.clearFields();
JOptionPane.showMessageDialog(this,"inserted sucess!","insert result",JOptionPane.INFORMATION_MESSAGE);
}
}
catch(NumberFormatException formatException){
JOptionPane.showMessageDialog(this,"bad age numver","invalid numver format",JOptionPane.ERROR_MESSAGE);
}
catch(SQLException ee)
{System.out.println(ee);
}
}
else JOptionPane.showMessageDialog(this,"bad sno number","invalid number format",JOptionPane.ERROR_MESSAGE);
}
//_____________________________________________________________________________________________________
}
//______________________________________________________________________________________________
//--------------------------------------------------------------------------StudentUI
public class StudentUI extends JPanel{
protected JLabel labels[];
protected JTextField fields[];
protected JButton doTask1,doTask2;
protected JPanel innerPanelCenter,innerPanelSouth;
protected int size;
public static final int SNO=0,SNAME=1,SSEX=2,SAGE=3,SDEPT=4;
public StudentUI(String arrayString[]){
size=arrayString.length;
labels=new JLabel[size];
fields=new JTextField[size];
for(int count=0;count<labels.length;count++)
labels[count]=new JLabel(arrayString[count]);
for(int count=0;count<fields.length;count++)
fields[count]=new JTextField();
innerPanelCenter=new JPanel();
innerPanelCenter.setLayout(new GridLayout(size,2));
for(int count=0;count<size;count++){
innerPanelCenter.add(labels[count]);
innerPanelCenter.add(fields[count]);
}
doTask1=new JButton();doTask2=new JButton();
innerPanelSouth=new JPanel();
innerPanelSouth.add(doTask1);
innerPanelSouth.add(doTask2);
setLayout(new BorderLayout());
add(innerPanelCenter,BorderLayout.CENTER);
add(innerPanelSouth,BorderLayout.SOUTH);
setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
validate();
}
public JButton getDotask1Button(){
return doTask1;
}
public JButton getDoTask2Button(){
return doTask2;
}
public JTextField[] getFields(){
return fields;
}
public void clearFields(){
for(int count=0;count<size;count++)
fields[count].setText("");
}
public void setFieldValues(String strings[])
throws IllegalArgumentException
{if(strings.length!=size)throw new IllegalArgumentException("there must be"+size+"strings in the array");
for(int count=0;count<size;count++)
fields[count].setText(strings[count]);
}
public String[] getFieldValues()
{
String values[]=new String[size];
for(int count=0;count<size;count++)
values[count]=fields[count].getText();
return values;
}
}
//_______________________________________________________________________________________________
}
评论