📄 phonebook.java
字号:
/*
* PhoneBook.java
*
* Created on 2008年3月13日, 下午4:46
*/
package pda;
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;
/**
*
* @author bzu
*/
public class PhoneBook extends javax.swing.JFrame {
/** Creates new form PhoneBook */
public PhoneBook() {
super("添加");
initComponents();
}
/** 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() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
nameField = new javax.swing.JTextField();
jLabel4 = new javax.swing.JLabel();
saveButton = new javax.swing.JButton();
phoneField = new javax.swing.JTextField();
emailField = new javax.swing.JTextField();
jLabel6 = new javax.swing.JLabel();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu2 = new javax.swing.JMenu();
saveMenuItem = new javax.swing.JMenuItem();
jLabel1.setFont(new java.awt.Font("宋体", 1, 18));
jLabel1.setText("\u8f93\u5165\u7535\u8bdd\u672c\u4fe1\u606f");
jLabel2.setFont(new java.awt.Font("宋体", 0, 15));
jLabel2.setText("\u59d3 \u540d\uff1a");
jLabel4.setFont(new java.awt.Font("宋体", 0, 15));
jLabel4.setText("\u7535 \u8bdd\uff1a");
saveButton.setText("\u4fdd\u5b58");
saveButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
saveButtonActionPerformed(evt);
}
});
jLabel6.setFont(new java.awt.Font("宋体", 0, 15));
jLabel6.setText("\u90ae \u7bb1\uff1a");
jMenu2.setText("\u7f16\u8f91");
saveMenuItem.setText("\u4fdd\u5b58");
saveMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
saveMenuItemActionPerformed(evt);
}
});
jMenu2.add(saveMenuItem);
jMenuBar1.add(jMenu2);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addGap(118, 118, 118)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 203, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(jLabel6)
.addComponent(jLabel4))
.addGap(20, 20, 20)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(nameField)
.addComponent(phoneField)
.addComponent(emailField, javax.swing.GroupLayout.DEFAULT_SIZE, 190, Short.MAX_VALUE)))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(saveButton, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(91, 91, 91)))))
.addGap(71, 71, 71))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(nameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(33, 33, 33)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(phoneField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel4))
.addGap(33, 33, 33)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(emailField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel6))
.addGap(15, 15, 15)
.addComponent(saveButton)
.addGap(21, 21, 21))
);
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width-400)/2, (screenSize.height-333)/2, 400, 333);
}// </editor-fold>//GEN-END:initComponents
private void saveMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveMenuItemActionPerformed
// TODO 将在此处添加您的处理代码:
this.setVisible(false);
update();
showPhoneBook spb=new showPhoneBook();
spb.setVisible(true);
}//GEN-LAST:event_saveMenuItemActionPerformed
private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
// TODO 将在此处添加您的处理代码:
this.setVisible(false);
update();
showPhoneBook spb=new showPhoneBook();
spb.setVisible(true);
}//GEN-LAST:event_saveButtonActionPerformed
/*public static void main(String args[]){
PhoneBook pb=new PhoneBook();
pb.setVisible(true);
}*/
public void update(){
file=new File("src/database/pda.mdb");//经典所在:不用配置数据源
path="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ="+file.getAbsolutePath();//获取绝对路径
String name,phone,email,up;
name=nameField.getText();
phone=phoneField.getText();
email=emailField.getText();
//up="INSERT INTO phonebook VALUES("+"'"+name+"','"+phone+"','"+email+"')";
up="INSERT INTO phonebook VALUES(?,?,?)";
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:pda");
//Connection con=DriverManager.getConnection(path);
//Statement stmt=con.createStatement();
//stmt.executeUpdate(up);
PreparedStatement pst=con.prepareStatement(up);
pst.setString(1,name);
pst.setString(2,phone);
pst.setString(3,email);
pst.executeUpdate();
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* @param args the command line arguments
*/
// 变量声明 - 不进行修改//GEN-BEGIN:variables
private javax.swing.JTextField emailField;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel6;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JTextField nameField;
private javax.swing.JTextField phoneField;
private javax.swing.JButton saveButton;
private javax.swing.JMenuItem saveMenuItem;
// 变量声明结束//GEN-END:variables
String path;
File file;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -