📄 modify_tushu.java
字号:
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 ISBN from book"); jcmbISBN.removeAllItems(); while(res.next()) { jcmbISBN.addItem(res.getString("ISBN")); } if(st!=null) st.close(); } catch(Exception ex){ JOptionPane.showConfirmDialog(null,"找不到数据库驱动程序!","Warning",JOptionPane.CLOSED_OPTION,JOptionPane.WARNING_MESSAGE); this.setVisible(false); } jcmbISBN.setSelectedItem(null); jtxtName.setText(""); jtxtAuthor.setText(""); jtxtPublish.setText(""); jtxtFuben.setText(""); jtxtKucun.setText(""); jlabelMsg.setText("选择图书ISBN号,然后点击修改。"); } private void jbtnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnAddActionPerformed // TODO add your handling code here: String flag="",is="",na="",au="",pu=""; int fu=0,ku=0; try{ try { is = new String((String) jcmbISBN.getSelectedItem()); } catch (Exception exception) { throw new NullPointerException(); } if(is.length()!=13) throw new ArithmeticException(); na=new String(jtxtName.getText().trim()); if(na.length()<1) throw new ArithmeticException(); au=new String(jtxtAuthor.getText().trim()); if(au.length()<1) throw new ArithmeticException(); pu=new String(jtxtPublish.getText().trim()); if(pu.length()<1) throw new ArithmeticException(); try{ ku=new Integer(Integer.parseInt(jtxtKucun.getText().trim())); }catch(NumberFormatException e){ throw new IOException(); } if(ku>2147483647||ku<0) throw new ArithmeticException(); fu=vFuben-vKucun+ku; if(fu>2147483647||fu<0) throw new ArithmeticException(); else if(ku>fu) throw new ArithmeticException(); 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 rs=st.executeQuery("select * from book"); while(rs.next()) if(rs.getString("ISBN").equals(is)) { flag=new String(rs.getString("name")); } if(flag.length()<1) throw new ArrayIndexOutOfBoundsException(); PreparedStatement ps_del=con.prepareStatement("delete from book where ISBN=?"); ps_del.setString(1,is); ps_del.executeUpdate(); PreparedStatement ps=con.prepareStatement("insert into book values(?,?,?,?,?,?)"); ps.setString(1, is); ps.setString(2, na); ps.setString(3, au); ps.setString(4, pu); ps.setInt(5, fu); ps.setInt(6,ku); ps.executeUpdate(); jlabelMsg.setText("修改图书信息成功!"); if(con!=null) con.close(); }catch(NullPointerException ex){ jlabelMsg.setText("请选择ISBN号!"); }catch(ArithmeticException e){ if(is.length()<1) jlabelMsg.setText("请选择ISBN号!"); else if(is.length()!=13) jlabelMsg.setText("ISBN格式不正确!"); else if(na.length()<1) jlabelMsg.setText("书名不能为空!"); else if(au.length()<1) jlabelMsg.setText("作者不能为空!"); else if(pu.length()<1) jlabelMsg.setText("出版社不能为空!"); else if(ku>2147483647||ku<0) jlabelMsg.setText("库存量错误"); else if(fu>2147483647||fu<0) jlabelMsg.setText("副本量错误!"); else if(ku>fu) jlabelMsg.setText("副本量不能小于库存量!"); }catch(ArrayIndexOutOfBoundsException e) { jlabelMsg.setText("此ISBN号书不存在!"); } catch(NumberFormatException e) { jlabelMsg.setText("副本量格式不正确!"); }catch(IOException e) { jlabelMsg.setText("库存量格式不正确!"); }catch(Exception e){ jlabelMsg.setText("修改失败!(可能连接数据库失败)"); } }//GEN-LAST:event_jbtnAddActionPerformed private void jcmbISBNActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcmbISBNActionPerformed // TODO add your handling code here: vFuben=0; vKucun=0; try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); }catch(ClassNotFoundException ex) { jlabelMsg.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); PreparedStatement ps=con.prepareStatement("select * from book where ISBN=?"); String is=new String((String)jcmbISBN.getSelectedItem()); if(is.length()<1) throw new ArithmeticException(); ps.setString(1, is); ResultSet rs=ps.executeQuery(); while(rs.next()){ jtxtName.setText(rs.getString("name")); jtxtAuthor.setText(rs.getString("author")); jtxtPublish.setText(rs.getString("publish")); vFuben=new Integer(Integer.parseInt(rs.getString("fubenNum"))); jtxtFuben.setText(String.valueOf(vFuben)); vKucun=new Integer(Integer.parseInt(rs.getString("kucunNum"))); jtxtKucun.setText(String.valueOf(vKucun)); } }catch(Exception ex) { jlabelMsg.setText("ERROR:可能连接数据库失败!"); } }//GEN-LAST:event_jcmbISBNActionPerformedprivate void jtxtKucunActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtxtKucunActionPerformed// TODO add your handling code here: int i=vFuben-vKucun; int j=new Integer(Integer.parseInt(jtxtKucun.getText())); jtxtFuben.setText(String.valueOf(i+j)); }//GEN-LAST:event_jtxtKucunActionPerformed /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { Modify_Tushu dialog = new Modify_Tushu(new javax.swing.JFrame(), true); dialog.addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent e) { System.exit(0); } }); dialog.setVisible(true); } }); } //自定义变量 private int vKucun=0,vFuben=0; // 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 jbtnAdd; private javax.swing.JComboBox jcmbISBN; private javax.swing.JLabel jlabelMsg; private javax.swing.JTextField jtxtAuthor; private javax.swing.JTextField jtxtFuben; private javax.swing.JTextField jtxtKucun; private javax.swing.JTextField jtxtName; private javax.swing.JTextField jtxtPublish; // End of variables declaration//GEN-END:variables }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -