📄 add.java
字号:
//add.java
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
class add extends JFrame {
private JButton qingchu, baocun;
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 column names
// set up GUI
public add()
{
super( "Add a record of students" );
initialize(); //connect to database
// create instance of reusable user interface
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);
qingchu=new JButton("清除");
baocun=new JButton("保存");
JPanel j1=new JPanel();
JPanel j2=new JPanel();
j1.add(no);
j1.add(not);
j1.add(name);
j1.add(namet);
j1.add(sex);
j1.add(sext);
j1.add(address);
j1.add(addresst);
j1.add(polity);
j1.add(polityt);
j1.add(health);
j1.add(healtht);
j1.add(compact);
j1.add(compactt);
j2.add(qingchu);
j2.add(baocun);
Container container=getContentPane();
container.setLayout(new BorderLayout());
j1.setLayout(new FlowLayout());
j2.setLayout(new FlowLayout());
container.add(j1,BorderLayout.CENTER);
container.add(j2,BorderLayout.SOUTH);
// register listener to call addRecord when button pressed
baocun.addActionListener(
// anonymous inner class to handle writeButton event
new ActionListener() {
// call addRecord when button pressed
public void actionPerformed( ActionEvent event )
{
addRecord();
}
} // end anonymous inner class
); // end call to addActionListener
// configure button doTask2 for use in this program
// register listener to call userInterface clearFields() when button pressed
qingchu.addActionListener(
// anonymous inner class to handle clearButton event
new ActionListener() {
// call userInterface clearFields() when button pressed
public void actionPerformed( ActionEvent event )
{
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 databse
}
} // end anonymous inner class
); // end call to addWindowListener
setSize( 380, 300 );
setVisible( true );
} // end of constructor
// connect to 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 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
// add record to file
public void addRecord()
{
//String fieldValues[] = userInterface.getFieldValues();
// if sno field value is not empty
if ( ! not.getText().equals( "" ) ) {
// output values to student
try {
String sqlInsert = "INSERT INTO people " +
"VALUES ('" +
not.getText() + "', '" +
namet.getText() +"', '"+
sext.getText()+ "', '"+
addresst.getText()+"', '"+
polityt.getText()+"', '"+
healtht.getText()+"', '"+
compactt.getText()+ "')";
int result = statement.executeUpdate(sqlInsert);
if (result!=0) {
not.setText("");
namet.setText("");
not.setText("");
sext.setText("");
addresst.setText("");
polityt.setText("");
healtht.setText("");
compactt.setText("");
JOptionPane.showMessageDialog( this,
"Inserted sucess!", "Insert Result",
JOptionPane.INFORMATION_MESSAGE );
}
} // end try
// process invalid age number
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 //if sno field value is empty
JOptionPane.showMessageDialog( this,
"Bad sno number ", "Invalid Number Format",
JOptionPane.ERROR_MESSAGE );
} // end method addRecord
public static void main( String args[] )
{
new add();
}
} // end AddStudentFrame class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -