📄 insertstaff.java
字号:
/*
* InsertStaff.java
*
* Created on 2007年1月19日, 上午4:56
*/
package javaapplication1;
import java.awt.Toolkit;
import java.sql.*;
/**
*
* @author lab
*/
public class InsertStaff extends javax.swing.JFrame {
/** Creates new form InsertStaff */
public InsertStaff() {
connectDB();
initComponents();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
//-------设置窗口位置居中---------------------------------------------------------------
int locationX=(int)(Toolkit.getDefaultToolkit().getScreenSize().getWidth()-getSize().getWidth())/2;
int locationY=(int)(Toolkit.getDefaultToolkit().getScreenSize().getHeight()-getSize().getHeight())/2;
setLocation(locationX,locationY);
//--------------------------------------------------------------------------------------
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" 生成的代码 ">//GEN-BEGIN:initComponents
private void initComponents() {
nameField = new javax.swing.JTextField();
salaryField = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
sexComboBox = new javax.swing.JComboBox();
jLabel3 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
confirmButton = new javax.swing.JButton();
cancelButton = new javax.swing.JButton();
resetButton = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
formWindowClosing(evt);
}
});
jLabel1.setText("\u59d3\u540d");
jLabel2.setText("\u6027\u522b");
sexComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "\u7537", "\u5973" }));
jLabel3.setText("\u85aa\u6c34");
jLabel5.setText("\u8bf7\u5f55\u5165\u5458\u5de5\u7684\u57fa\u672c\u8d44\u6599");
confirmButton.setText("\u786e\u5b9a");
confirmButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
confirmButtonActionPerformed(evt);
}
});
cancelButton.setText("\u53d6\u6d88");
cancelButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cancelButtonActionPerformed(evt);
}
});
resetButton.setText("\u91cd\u7f6e");
resetButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
resetButtonActionPerformed(evt);
}
});
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(89, 89, 89)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(confirmButton)
.add(27, 27, 27)
.add(resetButton)
.add(25, 25, 25)
.add(cancelButton)
.addContainerGap())
.add(layout.createSequentialGroup()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jLabel3)
.add(jLabel1)
.add(jLabel2))
.add(29, 29, 29)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
.add(org.jdesktop.layout.GroupLayout.LEADING, sexComboBox, 0, 133, Short.MAX_VALUE)
.add(org.jdesktop.layout.GroupLayout.LEADING, salaryField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 133, Short.MAX_VALUE)
.add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jLabel5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 133, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(nameField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 133, Short.MAX_VALUE))))
.add(98, 98, 98))))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(14, 14, 14)
.add(jLabel5)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jLabel1)
.add(nameField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jLabel2)
.add(sexComboBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(salaryField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(jLabel3))
.add(24, 24, 24)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(resetButton)
.add(cancelButton)
.add(confirmButton))
.addContainerGap())
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
// TODO 将在此处添加您的处理代码:
this.dispose();
try
{
insertStaff.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}//GEN-LAST:event_cancelButtonActionPerformed
//重置按钮响应事件
private void resetButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resetButtonActionPerformed
// TODO 将在此处添加您的处理代码:
nameField.setText("");
salaryField.setText("");
sexComboBox.setSelectedIndex(0);
}//GEN-LAST:event_resetButtonActionPerformed
//确定提交事件监听
private void confirmButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_confirmButtonActionPerformed
// TODO 将在此处添加您的处理代码:
String name = nameField.getText();
int salary = Integer.parseInt(salaryField.getText());
String sex = (sexComboBox.getSelectedItem()).toString();
CallableStatement proc = null;
try
{
proc = insertStaff.prepareCall("{ call InsertStaff(?,?,?) }");
proc.setString(1, name);
proc.setString(2, sex);
proc.setInt(3,salary);
proc.execute();
}
catch(Exception insertStaffException)
{
insertStaffException.printStackTrace();
}
finally
{
try
{
proc.close();
insertStaff.close();
}
catch(Exception closeInsertStaffException)
{
closeInsertStaffException.printStackTrace();
}
}
this.dispose();
}//GEN-LAST:event_confirmButtonActionPerformed
private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
// TODO 将在此处添加您的处理代码:
this.dispose();
}//GEN-LAST:event_formWindowClosing
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new InsertStaff().setVisible(true);
}
});
}
//连接数据库操作------------------------------
public void connectDB()
{
String dbUrl = "jdbc:odbc:afei";
String user = "scott";
String password = "tiger";
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
insertStaff = DriverManager.getConnection(dbUrl,user,password);
}
catch(Exception connectException)
{
connectException.printStackTrace();
}
}
// 变量声明 - 不进行修改//GEN-BEGIN:variables
private javax.swing.JButton cancelButton;
private javax.swing.JButton confirmButton;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel5;
private javax.swing.JTextField nameField;
private javax.swing.JButton resetButton;
private javax.swing.JTextField salaryField;
private javax.swing.JComboBox sexComboBox;
// 变量声明结束//GEN-END:variables
private Connection insertStaff;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -