📄 add_duzhe.java
字号:
/*
* Add_Duzhe.java
*
* Created on 2007年12月8日, 下午7:38
*/
import java.sql.*;
public class Add_Duzhe extends javax.swing.JDialog {
/** Creates new form Add_Duzhe */
public Add_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();
jLabel3 = new javax.swing.JLabel();
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();
jtxtlibCard = new javax.swing.JTextField();
jbtnQuery_Duzhe = new javax.swing.JButton();
jlabelMessage = new javax.swing.JLabel();
jtxtBirth = new javax.swing.JTextField();
jLabel7 = new javax.swing.JLabel();
jcmbGender = new javax.swing.JComboBox();
setTitle("添加读者信息");
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("借书数:");
jtxtBorrowNum.setText("0");
jtxtBorrowNum.setEnabled(false);
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));
jLabel7.setText("(格式:2000-10-12)");
jcmbGender.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "男", "女" }));
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(26, 26, 26)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(10, 10, 10)
.addComponent(jlabelMessage, javax.swing.GroupLayout.PREFERRED_SIZE, 283, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jLabel4)
.addGroup(jPanel1Layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jtxtName, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jcmbGender, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel6)
.addComponent(jLabel5)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jtxtlibCard)
.addGap(7, 7, 7)
.addComponent(jbtnQuery_Duzhe))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
.addComponent(jtxtBirth, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel7)))
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jtxtBorrowNum, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jtxtMajor, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 139, Short.MAX_VALUE)))))
.addContainerGap(18, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap(30, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jtxtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1)
.addComponent(jcmbGender, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(23, 23, 23)
.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)
.addComponent(jLabel7))
.addGap(16, 16, 16)
.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(23, 23, 23)
.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(21, 21, 21)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel6)
.addComponent(jbtnQuery_Duzhe)
.addComponent(jtxtlibCard, 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.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);
pack();
}// </editor-fold>//GEN-END:initComponents
public void Empty()
{
jtxtlibCard.setText("");
jtxtName.setText("");
jcmbGender.setSelectedItem(1);
jtxtBirth.setText("");
jtxtMajor.setText("");
jlabelMessage.setText("输入读者所有信息,然后点击添加。");
}
private void jbtnQuery_DuzheActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnQuery_DuzheActionPerformed
// TODO 将在此处添加您的处理代码:
String lc=null,na=null,mj="",sex="",bi="";
int bn=0;
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);
lc=new String(jtxtlibCard.getText().trim());
if(lc.length()!=8) throw new ArithmeticException();
na=new String(jtxtName.getText().trim());
if(na.length()>8||na.length()<1) throw new ArithmeticException();
sex=new String((String)jcmbGender.getSelectedItem());
if(sex.length()>2||sex.length()<1) throw new ArithmeticException();
bi=new String(jtxtBirth.getText().trim());
if(bi.length()<1||bi.length()>10) throw new ArithmeticException();
mj=new String(jtxtMajor.getText().trim());
if(mj.length()>12||mj.length()<1) throw new ArithmeticException();
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from XS");
while(rs.next())
if(lc.equals(rs.getString("libCard"))) throw new ArrayIndexOutOfBoundsException();
PreparedStatement ps=con.prepareStatement("insert into XS values(?,?,?,?,?,?)");
ps.setString(1,lc);
ps.setString(2,na);
ps.setString(3,mj);
ps.setString(4,sex);
ps.setString(5,bi);
ps.setInt(6,bn);
try {
ps.executeUpdate();
} catch (SQLException ex) {
throw new NumberFormatException();
}
jlabelMessage.setText("插入成功!");
st.close();
ps.close();
con.close();
}
catch(ArithmeticException ex)
{
if(lc.length()<1) jlabelMessage.setText("图书证号码不能为空!");
else if(lc.length()!=8) jlabelMessage.setText("图书证号码不是8位!");
else if(na.length()<1) jlabelMessage.setText("名字不能为空!");
else if(na.length()>8) jlabelMessage.setText("名字太长!");
else if(sex.length()<1) jlabelMessage.setText("性别不能为空!");
else if(sex.length()>2) jlabelMessage.setText("性别太长!");
else if(bi.length()<1) jlabelMessage.setText("出生时间不能为空!");
else if(bi.length()>10) jlabelMessage.setText("出生时间格式不正确!");
else if(mj.length()<1) jlabelMessage.setText("专业名不能为空!");
else if(mj.length()>12) jlabelMessage.setText("专业名太长!");
else if(bn>2147483647||bn<0) jlabelMessage.setText("借书数不正确!");
}
catch(NumberFormatException ex) {
jlabelMessage.setText("日期格式不正确!");
}
catch(ArrayIndexOutOfBoundsException ex) {
jlabelMessage.setText("此图书证号码已存在!");
}
catch(Exception ex) {
jlabelMessage.setText("插入失败!(可能连接数据库失败)");
}
}//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
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Add_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.JLabel jLabel7;
private javax.swing.JPanel jPanel1;
private javax.swing.JButton jbtnQuery_Duzhe;
private javax.swing.JComboBox jcmbGender;
private javax.swing.JLabel jlabelMessage;
private javax.swing.JTextField jtxtBirth;
private javax.swing.JTextField jtxtBorrowNum;
private javax.swing.JTextField jtxtMajor;
private javax.swing.JTextField jtxtName;
private javax.swing.JTextField jtxtlibCard;
// End of variables declaration//GEN-END:variables
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -