📄 showphonebook.java
字号:
/*
* showPhoneBook.java
*
* Created on 2008年4月10日, 下午2:23
*/
package pda;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;
import java.sql.*;
import java.sql.DriverManager;
import java.sql.ResultSet;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
/**
*
* @author Administrator
*/
public class showPhoneBook extends javax.swing.JFrame {
/** Creates new form showPhoneBook */
public showPhoneBook() {
super("电话本");
initComponents();
sql="select * from phonebook";
connect();
}
/** 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() {
del = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
search = new javax.swing.JButton();
searchField = new javax.swing.JTextField();
jComboBox1 = new javax.swing.JComboBox();
delButton = new javax.swing.JButton();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
addMenuItem = new javax.swing.JMenuItem();
chMenuItem = new javax.swing.JMenuItem();
del.setText("\u6309\u59d3\u540d\u5220\u9664");
del.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
delActionPerformed(evt);
}
});
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
search.setText("\u67e5\u8be2");
search.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
searchActionPerformed(evt);
}
});
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "\u6309\u59d3\u540d\u67e5\u8be2", "\u7535\u8bdd\u6a21\u7cca\u67e5\u8be2" }));
jComboBox1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jComboBox1ActionPerformed(evt);
}
});
delButton.setText("\u6309\u59d3\u540d\u5220\u9664");
delButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
delButtonActionPerformed(evt);
}
});
jMenu1.setText("\u529f\u80fd");
addMenuItem.setText("\u6dfb\u52a0");
addMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
addMenuItemActionPerformed(evt);
}
});
jMenu1.add(addMenuItem);
chMenuItem.setText("\u4fee\u6539");
chMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
chMenuItemActionPerformed(evt);
}
});
jMenu1.add(chMenuItem);
jMenuBar1.add(jMenu1);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 372, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(searchField, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(search)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(delButton)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(searchField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(search)
.addComponent(delButton))
.addContainerGap(25, Short.MAX_VALUE))
);
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width-400)/2, (screenSize.height-300)/2, 400, 300);
}// </editor-fold>//GEN-END:initComponents
private void chMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chMenuItemActionPerformed
// TODO 将在此处添加您的处理代码:
this.setVisible(false);
ChangePhoneBook cpb=new ChangePhoneBook();
cpb.setVisible(true);
}//GEN-LAST:event_chMenuItemActionPerformed
private void delButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_delButtonActionPerformed
// TODO 将在此处添加您的处理代码:
try {
file=new File("src/database/pda.mdb");//经典所在:不用配置数据源
path="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ="+file.getAbsolutePath();//获取绝对路径
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:pda");
//Connection con=DriverManager.getConnection(path);
Statement stmt=con.createStatement();
varname=searchField.getText();
String delstr="delete from phonebook where name="+"'"+varname+"'";
stmt.executeUpdate(delstr);
stmt.close();
con.close();
searchField.setText(null);
sql="select * from phonebook";
connect();
} catch (Exception ex) {
ex.printStackTrace();
}
}//GEN-LAST:event_delButtonActionPerformed
private void addMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addMenuItemActionPerformed
// TODO 将在此处添加您的处理代码:
this.setVisible(false);
PhoneBook pb=new PhoneBook();
pb.setVisible(true);
}//GEN-LAST:event_addMenuItemActionPerformed
private void delActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_delActionPerformed
// TODO 将在此处添加您的处理代码:
}//GEN-LAST:event_delActionPerformed
private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
// TODO 将在此处添加您的处理代码:
JComboBox cb = (JComboBox)evt.getSource();
String checkBoxName = (String)cb.getSelectedItem();
if(checkBoxName.equals("按姓名查询")){
fag=1;
}
else if(checkBoxName.equals("电话模糊查询")){
fag=0;
}
}//GEN-LAST:event_jComboBox1ActionPerformed
private void searchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchActionPerformed
// TODO 将在此处添加您的处理代码:
if(fag==1){
varname=searchField.getText();
sql="select * from phonebook where name="+"'"+varname+"'";
connect();
}
else if(fag==0){
varname=searchField.getText();
sql="select * from phonebook where phone like "+"'%"+varname+"%'";
connect();
}
}//GEN-LAST:event_searchActionPerformed
/**
* @param args the command line arguments
*/
public void connect(){
jTextArea1.setText(null);
try {
file=new File("src/database/pda.mdb");//经典所在:不用配置数据源
path="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ="+file.getAbsolutePath();//获取绝对路径
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
java.sql.Connection con=DriverManager.getConnection("jdbc:odbc:pda");
//java.sql.Connection con=DriverManager.getConnection(path);
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()){
String name=rs.getString(1);
String phone=rs.getString(2);
String email=rs.getString(3);
jTextArea1.append("姓名:"+name+" 电话:"+phone+" E-mail:"+email+"\n");
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
/*public static void main(String args[]) {
showPhoneBook spb=new showPhoneBook();
//spb.connect();
spb.setVisible(true);
}*/
// 变量声明 - 不进行修改//GEN-BEGIN:variables
private javax.swing.JMenuItem addMenuItem;
private javax.swing.JMenuItem chMenuItem;
private javax.swing.JButton del;
private javax.swing.JButton delButton;
private javax.swing.JComboBox jComboBox1;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JButton search;
private javax.swing.JTextField searchField;
// 变量声明结束//GEN-END:variables
String sql,varname,path;
int fag=1;
File file;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -