//Update.java
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
class update extends JFrame {
private JButton queren,qingchu, querenxiugai,fangqi;
private JLabel sno,no,name,sex,address,polity,health,compact;
private JTextField snot,namet,not,sext,addresst,polityt,healtht,compactt;
// JDBC driver and database URL
static final String JDBC_DRIVER = "sun.jdbc.odbc.JdbcOdbcDriver";
static final String DATABASE_URL = "jdbc:odbc:RSGLDSN";
// declare Connection and Statement for accessing
// and querying database
private Connection connection;
private Statement statement;
String sqlString ;
// set up GUI
public update()
{
super( "Update a record of students" );
initialize(); //connect database
sno=new JLabel("请输入要修改的工号:");
snot=new JTextField(20);
no=new JLabel(" 工 号: ");
not=new JTextField(20);
name=new JLabel("姓 名: ");
sex=new JLabel("性 别: ");
address=new JLabel("地 址: " );
polity=new JLabel("政治面貌: ");
health=new JLabel("生理状况: ");
compact=new JLabel("合 同: ");
not=new JTextField(20);
namet=new JTextField(20);
sext=new JTextField(20);
addresst=new JTextField(20);
polityt=new JTextField(20);
healtht=new JTextField(20);
compactt=new JTextField(20);
queren=new JButton("确定");
qingchu=new JButton("清除");
querenxiugai=new JButton("确认修改");
fangqi=new JButton("放弃");
JPanel j1=new JPanel();
JPanel j2=new JPanel();
JPanel j3=new JPanel();
JPanel j4=new JPanel();
JPanel j5=new JPanel();
j4.add(sno);
j4.add(snot);
j5.add(queren);
j5.add(qingchu);
j2.add(no);
j2.add(not);
j2.add(name);
j2.add(namet);
j2.add(sex);
j2.add(sext);
j2.add(address);
j2.add(addresst);
j2.add(polity);
j2.add(polityt);
j2.add(health);
j2.add(healtht);
j2.add(compact);
j2.add(compactt);
j3.add(querenxiugai);
j3.add(fangqi);
Container container=getContentPane();
container.setLayout(new BorderLayout());
j1.setLayout(new BorderLayout());
j2.setLayout(new FlowLayout());
j3.setLayout(new FlowLayout());
j4.setLayout(new FlowLayout());
j5.setLayout(new FlowLayout());
j1.add(j4,BorderLayout.CENTER);
j1.add(j5,BorderLayout.SOUTH);
container.add(j1,BorderLayout.NORTH);
container.add(j2,BorderLayout.CENTER);
container.add(j3,BorderLayout.SOUTH);
// register listener to call firstButton1 when button pressed
queren.addActionListener(
// anonymous inner class to handle firstButton1 event
new ActionListener() {
// call DisplayRecord() when firstButton1 pressed
public void actionPerformed( ActionEvent event )
{
DisplayRecord();
}
} // end anonymous inner class
); // end call to addActionListener
// configure button doTask2 for userInterface1 in this program
// register listener to call serInterface1.clearFields() when secondButton1 pressed
qingchu.addActionListener(
// anonymous inner class to handle secondButton1 event
new ActionListener() {
// call addRecord when button pressed
public void actionPerformed( ActionEvent event )
{
snot.setText("");
not.setText("");
namet.setText("");
not.setText("");
sext.setText("");
addresst.setText("");
polityt.setText("");
healtht.setText("");
compactt.setText("");
}
} // end anonymous inner class
);
// register listener to call UpdateRecord() when button pressed
querenxiugai.addActionListener(
// anonymous inner class to handle firstButton2 event
new ActionListener() {
// call UpdateRecord(); when button pressed
public void actionPerformed( ActionEvent event )
{
//initialize();
UpdateRecord();
}
} // end anonymous inner class
); // end call to addActionListener
// register listener to call userInterface2.clearFields() when button pressed
fangqi.addActionListener(
// anonymous inner class to handle secondButton2 event
new ActionListener() {
// call addRecord when button pressed
public void actionPerformed( ActionEvent event )
{
snot.setText("");
not.setText("");
namet.setText("");
not.setText("");
sext.setText("");
addresst.setText("");
polityt.setText("");
healtht.setText("");
compactt.setText("");
}
} // end anonymous inner class
); // end call to addActionListener
// register window listener to handle window closing event
addWindowListener(
// anonymous inner class to handle windowClosing event
new WindowAdapter() {
// add current record in GUI to file, then close file
public void windowClosing( WindowEvent event )
{
terminate();//close connectin
}
} // end anonymous inner class
); // end call to addWindowListener
setSize( 380, 300 );
setVisible( true );
} // end constructor
//disconnect database
public void initialize()
{
try {
Class.forName( JDBC_DRIVER );
// establish connection to database
connection = DriverManager.getConnection( DATABASE_URL,"sa",null );
// create Statement for querying database
statement = connection.createStatement();
}
catch ( SQLException sqlException ) {
JOptionPane.showMessageDialog( null, sqlException.getMessage(),
"Database Error", JOptionPane.ERROR_MESSAGE );
System.exit( 1 );
}
// detect problems loading database driver
catch ( ClassNotFoundException classNotFound ) {
JOptionPane.showMessageDialog( null, classNotFound.getMessage(),
"Driver Not Found", JOptionPane.ERROR_MESSAGE );
System.exit( 1 );
}
} // end method openFile
// close file and terminate application
public void terminate()
{
// close file
try {
statement.close();
connection.close();
}
// handle exceptions closing statement and connection
catch ( SQLException sqlException ) {
JOptionPane.showMessageDialog( null,
sqlException.getMessage(), "Database Error",
JOptionPane.ERROR_MESSAGE );
System.exit( 1 );
}
} // end method
public void DisplayRecord()
{
if ( ! snot.getText().equals( "" ) ) {
// display values from student table
try {
String sqlString = "select * from people "+
" where no='"+snot.getText() + "'";
ResultSet resultSet =statement.executeQuery( sqlString) ;
if ( resultSet.next() ) {
not.setText(resultSet.getString( 1 )) ;
namet.setText(resultSet.getString( 2 )) ;
sext.setText(resultSet.getString( 3 )) ;
addresst.setText(resultSet.getString( 4 )) ;
polityt.setText(resultSet.getString( 5 )) ;
healtht.setText(resultSet.getString( 6 )) ;
compactt.setText(resultSet.getString( 7 )) ;
}
else
{
snot.setText("");
not.setText("");
namet.setText("");
not.setText("");
sext.setText("");
addresst.setText("");
polityt.setText("");
healtht.setText("");
compactt.setText("");
JOptionPane.showMessageDialog( this,
"Not fund this record!", "Find Result",
JOptionPane.INFORMATION_MESSAGE );
}
} // end try
// process invalid age number format
catch ( NumberFormatException formatException ) {
JOptionPane.showMessageDialog( this,
"Bad age number ", "Invalid Number Format",
JOptionPane.ERROR_MESSAGE );
}
// process exceptions from sql
catch (SQLException ee)
{ System.out.println(ee); }
} //end of if sno field value is not empty
else //if sno field value is empty
JOptionPane.showMessageDialog( this,
"Bad sno number ", "Invalid Number Format",
JOptionPane.ERROR_MESSAGE );
}
// Update record of student
public void UpdateRecord()
{
// if sno field value is not empty
if ( ! snot.getText().equals( "" ) ) {
// update values from student
try {
String sqlString = "Update people set "+
"no='"+not.getText() + "',"+
"name='"+namet.getText()+ "',"+
"sex='"+sext.getText() +"', "+
"address='"+addresst.getText() +"', "+
"polity='"+polityt.getText()+"', "+
"health='"+healtht.getText()+"', "+
"compact='"+compactt.getText()+
"' where no='"+snot.getText()+ "'";
int result = statement.executeUpdate(sqlString);
if (result!=0) {
JOptionPane.showMessageDialog( this,
"Updated sucess!", "Update Result",
JOptionPane.INFORMATION_MESSAGE );
}
} // end try
// process invalid age number format
catch ( NumberFormatException formatException ) {
JOptionPane.showMessageDialog( this,
"Bad age number ", "Invalid Number Format",
JOptionPane.ERROR_MESSAGE );
}
// process exceptions from sql
catch (SQLException ee)
{ System.out.println(ee); }
} //end of if sno field value is not empty
else // if sno field value is empty
JOptionPane.showMessageDialog( this,
"Bad sno number ", "Invalid Number Format",
JOptionPane.ERROR_MESSAGE );
} // end method updateRecord
public static void main( String args[] )
{
new update();
}
} // end class