📄 deletecustomerframe.java
字号:
//DeletecustomerFrame.java
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
import UI.customerUI;
class DeletecustomerFrame extends JFrame {
private customerUI userInterface1,userInterface2;
private JButton firstButton1,secondButton1, firstButton2,secondButton2;
String snoUpdate;
// JDBC driver and database URL
static final String JDBC_DRIVER = "sun.jdbc.odbc.JdbcOdbcDriver";
static final String DATABASE_URL = "jdbc:odbc:PostofficeDSN";
// declare Connection and Statement for accessing
// and querying database
private Connection connection;
private Statement statement;
String sqlString ;
// set up GUI
public DeletecustomerFrame()
{
super( "Delete records from students" );
initialize(); //connect database
// create instance of reusable user interface
//set up column names
String names1[] = { "请输入要删除的客户的代码:"};
userInterface1= new customerUI( names1 ); // four textfields
//set up column names
String names2[] = { "客户姓名","电话","地址","邮编","客户代码"};
userInterface2 = new customerUI(names2 );
Container c= getContentPane();
Box box = Box.createVerticalBox();
box.add(userInterface1 );
box.add(userInterface2 );
c.add(box);
firstButton1 = userInterface1.getDoTask1Button();
firstButton1.setText( "确认" );
// register listener to call openFile when firstButton1 pressed
firstButton1.addActionListener(
// anonymous inner class to handle firstButton2 event
new ActionListener() {
// call DisplayRecord() when button pressed
public void actionPerformed( ActionEvent event )
{
//initialize();
DisplayRecord();
}
} // end anonymous inner class
); // end call to addActionListener
// configure button doTask2 for userInterface1 in this program
secondButton1 = userInterface1.getDoTask2Button();
secondButton1.setText( "清除" );
// register listener to call clearFields when button pressed
secondButton1.addActionListener(
// anonymous inner class to handle secondButton1 event
new ActionListener() {
// call clearFields when button pressed
public void actionPerformed( ActionEvent event )
{
userInterface1.clearFields();
}
} // end anonymous inner class
);
// configure button doTask1 for userInterface2 in this program
firstButton2 = userInterface2.getDoTask1Button();
firstButton2.setText( "确认删除" );
// register listener to call firstButton2 when button pressed
firstButton2.addActionListener(
// anonymous inner class to handle firstButton2 event
new ActionListener() {
// call UpdateRecord when button pressed
public void actionPerformed( ActionEvent event )
{
UpdateRecord();
}
} // end anonymous inner class
); // end call to addActionListener
// configure button doTask2 for serInterface2 in this program
secondButton2 = userInterface2.getDoTask2Button();
secondButton2.setText( "放弃" );
// register listener to call clearFields when button pressed
secondButton2.addActionListener(
// anonymous inner class to handle secondButton2 event
new ActionListener() {
// call addRecord when button pressed
public void actionPerformed( ActionEvent event )
{
userInterface2.clearFields();
}
} // 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( 400, 260 );
setVisible( true );
} // end constructor
//disconnect database
public void initialize()
{
try {
Class.forName( JDBC_DRIVER );
// establish connection to database
connection = DriverManager.getConnection( DATABASE_URL,"sa","123" );
// 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 database
public void terminate()
{
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()
{
String fieldValues1[] = userInterface1.getFieldValues();
String fieldValues2 []=new String[5] ;
if ( ! fieldValues1[ customerUI.客户姓名 ].equals( "" ) ) {
snoUpdate=fieldValues1[0];
// display values from file
try {
int numberAge = Integer.parseInt(
fieldValues1[ customerUI.客户姓名 ] );
String sqlString = "select * from customer "+
" where 客户代码='"+fieldValues1[0] + "'";
ResultSet resultSet =statement.executeQuery( sqlString) ;
ResultSetMetaData metaData = resultSet.getMetaData();
int numberOfColumns = metaData.getColumnCount();
if ( resultSet.next() ) {
fieldValues2[0]=resultSet.getString( 1 ) ;
fieldValues2[1]=resultSet.getString( 2 ) ;
fieldValues2[2]=resultSet.getString( 3 ) ;
fieldValues2[3]=String.valueOf(resultSet.getInt( 4 )) ;
fieldValues2[4]=resultSet.getString( 5 ) ;
userInterface2.setFieldValues(fieldValues2);
}
else
{
userInterface2.clearFields();
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 file output
catch (SQLException ee)
{ System.out.println(ee); }
}
//end of if sno field value is not empty
else
JOptionPane.showMessageDialog( this,
"Bad sno number ", "Invalid Number Format",
JOptionPane.ERROR_MESSAGE );
}
// Delete record of student
public void UpdateRecord()
{
String fieldValues[] = userInterface2.getFieldValues();
// if sno field value is not empty
if ( ! fieldValues[ customerUI.客户姓名 ].equals( "" ) ) {
// delete record from student
try {
int numberAge = Integer.parseInt(
fieldValues[ customerUI.邮编 ] );
//the string sql statement
String sqlString = "delete from customer "+
"where 客户代码='"+snoUpdate + "'";
int result = statement.executeUpdate(sqlString);
if (result!=0) {
JOptionPane.showMessageDialog( this,
"Deleted sucess!", "Delete Result",
JOptionPane.INFORMATION_MESSAGE );
}
} // end try
// process invalid age format
catch ( NumberFormatException formatException ) {
JOptionPane.showMessageDialog( this,
"Bad age number ", "Invalid Number Format",
JOptionPane.ERROR_MESSAGE );
}
// process exceptions for sql
catch (SQLException ee)
{ System.out.println(ee); }
} //end of if sno field value is not empty
else
JOptionPane.showMessageDialog( this,
"Bad sno number ", "Invalid Number Format",
JOptionPane.ERROR_MESSAGE );
} // end method updateRecord
public static void main( String args[] )
{
new DeletecustomerFrame();
}
} // end class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -