📄 query_duzhe.java
字号:
/** NewDialog.java
*
* Created on 2007年12月4日, 下午7:17
*/
import java.sql.*;
import java.util.*;
import javax.swing.JOptionPane;
/**
*
* @author Administrator
*/
public class Query_Duzhe extends javax.swing.JDialog {
/** Creates new form NewDialog */
public Query_Duzhe(javax.swing.JFrame parent, boolean modal) {
super(parent, modal);
initComponents();
this.setSize(400,300);
this.setLocation(200,220);
this.setResizable(false);
}
/** 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="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jtxtName = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
jtxtGender = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
jtxtBirth = new javax.swing.JTextField();
jLabel4 = new javax.swing.JLabel();
jtxtMajor = new javax.swing.JTextField();
jLabel5 = new javax.swing.JLabel();
jtxtBorrowNum = new javax.swing.JTextField();
jLabel6 = new javax.swing.JLabel();
jbtnQuery_Duzhe = new javax.swing.JButton();
jlabelMessage = new javax.swing.JLabel();
jcmblibCard = new javax.swing.JComboBox();
setTitle("查询读者信息");
setResizable(false);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
closeDialog(evt);
}
});
jLabel1.setText("姓名:");
jLabel2.setText("性别:");
jLabel3.setText("出生时间:");
jLabel4.setText("专业名: ");
jLabel5.setText("借书数:");
jLabel6.setText("请输入借书证号:");
jbtnQuery_Duzhe.setText("查询");
jbtnQuery_Duzhe.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jbtnQuery_DuzheActionPerformed(evt);
}
});
jlabelMessage.setFont(new java.awt.Font("宋体", 0, 18));
jcmblibCard.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jcmblibCardActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(25, 25, 25)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
.addComponent(jLabel5)
.addGap(22, 22, 22)
.addComponent(jtxtBorrowNum, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jtxtName, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(27, 27, 27)
.addComponent(jLabel2)
.addGap(13, 13, 13)
.addComponent(jtxtGender, javax.swing.GroupLayout.DEFAULT_SIZE, 59, Short.MAX_VALUE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel4))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jtxtMajor, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jtxtBirth, javax.swing.GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE))
.addGap(72, 72, 72)))
.addGap(16, 16, 16)))
.addGap(34, 34, 34))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(20, 20, 20)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jlabelMessage, javax.swing.GroupLayout.PREFERRED_SIZE, 293, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel6)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jcmblibCard, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jbtnQuery_Duzhe)))
.addContainerGap(34, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(29, 29, 29)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jtxtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1)
.addComponent(jLabel2)
.addComponent(jtxtGender, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(jtxtBirth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(25, 25, 25)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(jtxtMajor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(20, 20, 20)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(jtxtBorrowNum, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(17, 17, 17)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel6)
.addComponent(jbtnQuery_Duzhe)
.addComponent(jcmblibCard, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jlabelMessage, javax.swing.GroupLayout.DEFAULT_SIZE, 29, Short.MAX_VALUE)
.addContainerGap())
);
getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);
pack();
}// </editor-fold>//GEN-END:initComponents
public void Empty()
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(ClassNotFoundException ex)
{
jlabelMessage.setText("SQL错:"+ex.getMessage());
}
try {
String url="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=sources\\data.mdb;pwd=shujuyuan";
Connection con=DriverManager.getConnection(url);
Statement st=con.createStatement();
ResultSet res=st.executeQuery("select libCard from XS");
jcmblibCard.removeAllItems();
while(res.next()) {
jcmblibCard.addItem(res.getString("libCard"));
}
st.close();
} catch(Exception ex){
JOptionPane.showConfirmDialog(null,"找不到数据库驱动程序!","Warning",JOptionPane.CLOSED_OPTION,JOptionPane.WARNING_MESSAGE);
this.setVisible(false);
}
jcmblibCard.setSelectedItem(0);
jtxtName.setText("");
jtxtGender.setText("");
jtxtBirth.setText("");
jtxtMajor.setText("");
jtxtBorrowNum.setText("");
jlabelMessage.setText("输入读者借书证号,然后点击查询。");
jtxtName.setEnabled(false);
jtxtGender.setEnabled(false);
jtxtBirth.setEnabled(false);
jtxtMajor.setEnabled(false);
jtxtBorrowNum.setEnabled(false);
jtxtName.setEnabled(false);
}
private void jbtnQuery_DuzheActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnQuery_DuzheActionPerformed
// TODO 将在此处添加您的处理代码:
jlabelMessage.setText("");
jtxtName.setText("");
jtxtGender.setText("");
jtxtBirth.setText("");
jtxtMajor.setText("");
jtxtBorrowNum.setText("");
String flag="",lc="";
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(ClassNotFoundException ex)
{
jlabelMessage.setText("SQL错:"+ex.getMessage());
}
try{
// Connection con=DriverManager.getConnection("jdbc:odbc:xsbook");
String url="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=sources\\data.mdb;pwd=shujuyuan";
Connection con=DriverManager.getConnection(url);
PreparedStatement ps=con.prepareStatement("select * from XS where libCard=?");
lc=new String((String)jcmblibCard.getSelectedItem());
if(lc.length()<1) throw new ArrayIndexOutOfBoundsException();
ps.setString(1,lc);
ResultSet rs=ps.executeQuery();
// if(rs.isLast()) throw new SQLException();
while(rs.next())
{
flag=new String(rs.getString("name"));
jtxtName.setText(flag);
jtxtMajor.setText(rs.getString("major"));
jtxtGender.setText(rs.getString("gender"));
jtxtBirth.setText(rs.getString("birth").substring(0, 10));
jtxtBorrowNum.setText(rs.getString("borrowNum"));
}
if(flag.length()<1) throw new ArrayIndexOutOfBoundsException();
ps.close();
// rs.close();
con.close();
jlabelMessage.setText("查询成功!");
jtxtName.setEnabled(true);
jtxtGender.setEnabled(true);
jtxtBirth.setEnabled(true);
jtxtMajor.setEnabled(true);
jtxtBorrowNum.setEnabled(true);
jtxtName.setEnabled(true);
}
catch(ArrayIndexOutOfBoundsException ex){
if(lc.length()<1) jlabelMessage.setText("请选择借书证号!");
else if(flag.length()<1) jlabelMessage.setText("此借书证号不存在!");
}
catch(Exception ex){
jlabelMessage.setText("查询失败!(可能连接数据库失败)");
// ex.printStackTrace();
}
}//GEN-LAST:event_jbtnQuery_DuzheActionPerformed
/** Closes the dialog */
private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
setVisible(false);
dispose();
}//GEN-LAST:event_closeDialog
private void jcmblibCardActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcmblibCardActionPerformed
// TODO add your handling code here:
jtxtName.setText("");
jtxtGender.setText("");
jtxtBirth.setText("");
jtxtMajor.setText("");
jtxtBorrowNum.setText("");
jlabelMessage.setText("输入读者借书证号,然后点击查询。");
jtxtName.setEnabled(false);
jtxtGender.setEnabled(false);
jtxtBirth.setEnabled(false);
jtxtMajor.setEnabled(false);
jtxtBorrowNum.setEnabled(false);
jtxtName.setEnabled(false);
}//GEN-LAST:event_jcmblibCardActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Query_Duzhe(new javax.swing.JFrame(), true).setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JPanel jPanel1;
private javax.swing.JButton jbtnQuery_Duzhe;
private javax.swing.JComboBox jcmblibCard;
private javax.swing.JLabel jlabelMessage;
private javax.swing.JTextField jtxtBirth;
private javax.swing.JTextField jtxtBorrowNum;
private javax.swing.JTextField jtxtGender;
private javax.swing.JTextField jtxtMajor;
private javax.swing.JTextField jtxtName;
// End of variables declaration//GEN-END:variables
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -