NetBeans數據庫筆記---三層架構
阿新 • • 發佈:2018-08-05
ima finally 如果 run from new option etc nds
1.創建數據庫,數據表
用MySQL數據庫和Navicat for MySQL工具創建表
2.創建實體類——反應表結構(列——變量)
也就是對應表建立的gets和sets方法,實體類的名字一般都與數據庫表的名字相同
3.創建數據訪問層。
1.BaseDAO(父類)代碼:
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class BaseDao { protected Connection conn = null; protected PreparedStatement pst = null; protected ResultSet rs = null; private String Driver = "com.mysql.jdbc.Driver"; private String url = "jdbc:mysql://localhost:3306/apple";// 這裏apple是數據庫名 private String user = "root"; private String password = "root"; public void OpenConnection() throws ClassNotFoundException, SQLException { Class.forName(Driver); conn = DriverManager.getConnection(url, user, password); } public void CloseAll() throws SQLException { if (rs != null) { rs.close(); } if (pst != null) { pst.close(); } if (conn != null && conn.isClosed() == false) { conn.close(); } } }
2.XXXDAO(子類)(一個表一個dao類)
1.如果返回多條數據,對應ArrayList集合類型
2.方法的參數,是sql執行的條件where 有條件,傳參;where沒有條件,不用傳參
public class LotForDAO extends ConnDAO{ public int insert(LotInfor lot){ int result=0; try { super.openConn(); String sql="insert into LotInfor(lotType,lotNum1,lotNum2,lotNum3,lotNum4,lotNum5,lotNum6,lotNum7,lotTerm) values(?,?,?,?,?,?,?,?,?)"; super.psm = super.conn.prepareStatement(sql); psm.setString(1, lot.getLotType()); psm.setString(2, lot.getLotNum1()); psm.setString(3, lot.getLotNum2()); psm.setString(4, lot.getLotNum3()); psm.setString(5, lot.getLotNum4()); psm.setString(6, lot.getLotNum5()); psm.setString(7, lot.getLotNum6()); psm.setString(8, lot.getLotNum7()); psm.setString(9, lot.getLotTerm()); result=psm.executeUpdate(); } catch (ClassNotFoundException ex) { Logger.getLogger(LotForDAO.class.getName()).log(Level.SEVERE, null, ex); } catch (SQLException ex) { Logger.getLogger(LotForDAO.class.getName()).log(Level.SEVERE, null, ex); }finally{ try { super.closeConn(); } catch (SQLException ex) { Logger.getLogger(LotForDAO.class.getName()).log(Level.SEVERE, null, ex); } } return result; } public int delete(String lotTerm){ int result=0; try { super.openConn(); String sql="delete from LotInfor where lotTerm=?"; super.psm=super.conn.prepareStatement(sql); psm.setString(1, lotTerm); result=psm.executeUpdate(); } catch (ClassNotFoundException ex) { Logger.getLogger(LotForDAO.class.getName()).log(Level.SEVERE, null, ex); } catch (SQLException ex) { Logger.getLogger(LotForDAO.class.getName()).log(Level.SEVERE, null, ex); }finally{ try { super.closeConn(); } catch (SQLException ex) { Logger.getLogger(LotForDAO.class.getName()).log(Level.SEVERE, null, ex); } } return result; } }
4.業務邏輯(省略)
5.表示層(前端頁面swing/jsp)
線程的代碼:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package apputil; import appframe.JFrameMain; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JOptionPane; /** * * @author wjw */ public class MyThread implements Runnable { JLabel[] arr; JButton jbu; JFrameMain jf; ImageIcon shan = new ImageIcon(this.getClass().getResource("../appimg/red2.gif")); ImageIcon jing = new ImageIcon(this.getClass().getResource("../appimg/red1.gif")); public MyThread(JLabel[] arr, JButton jbu,JFrameMain jf) { this.arr = arr; this.jbu = jbu; this.jf=jf; } public void run() { this.jbu.setEnabled(false); int random = (int) (Math.random() * 200) + 40; int sleep = 550; for (int i = 0; i <= random; i++) { arr[JFrameMain.index].setIcon(jing); if (JFrameMain.index >= 23) { JFrameMain.index = -1; } arr[JFrameMain.index + 1].setIcon(shan); JFrameMain.index = JFrameMain.index + 1; if (i < 10) { sleep = sleep - 50; } if (i + 10 > random) { sleep = sleep + 50; } try { Thread.sleep(sleep); } catch (InterruptedException ex) { Logger.getLogger(MyThread.class.getName()).log(Level.SEVERE, null, ex); } } int fenshu = Integer.parseInt(jf.jguageNum.getText()); int num1 = Integer.parseInt(jf.jLabel36.getText()); int num2 = Integer.parseInt(jf.jLabel37.getText()); int num3 = Integer.parseInt(jf.jLabel38.getText()); int num4 = Integer.parseInt(jf.jLabel39.getText()); int num5 = Integer.parseInt(jf.jLabel48.getText()); int num6 = Integer.parseInt(jf.jLabel49.getText()); int num7 = Integer.parseInt(jf.jLabel50.getText()); int num8 = Integer.parseInt(jf.jLabel51.getText()); switch (JFrameMain.index) { case 5: case 10: case 16: case 22: if(num8>0){ jf.jguageNum.setText(Integer.toString(fenshu + 5 * num8)); JOptionPane.showMessageDialog(null, "恭喜,您運氣真好!", "提示", JOptionPane.INFORMATION_MESSAGE); }else{ JOptionPane.showMessageDialog(null, "繼續努力,謝謝", "提示", JOptionPane.INFORMATION_MESSAGE); } break; case 0: case 11: case 12: if(num7>0){ jf.jguageNum.setText(Integer.toString(fenshu + 10 * num7)); JOptionPane.showMessageDialog(null, "恭喜,您運氣真好!", "提示", JOptionPane.INFORMATION_MESSAGE); }else{ JOptionPane.showMessageDialog(null, "繼續努力,謝謝", "提示", JOptionPane.INFORMATION_MESSAGE); } break; case 6: case 17: case 18: if(num6>0){ jf.jguageNum.setText(Integer.toString(fenshu + 10 * num6)); JOptionPane.showMessageDialog(null, "恭喜,您運氣真好!", "提示", JOptionPane.INFORMATION_MESSAGE); } else{ JOptionPane.showMessageDialog(null, "繼續努力,謝謝", "提示", JOptionPane.INFORMATION_MESSAGE); } break; case 1: case 13: case 23: if(num5>0){ jf.jguageNum.setText(Integer.toString(fenshu + 10 * num5)); JOptionPane.showMessageDialog(null, "恭喜,您運氣真好!", "提示", JOptionPane.INFORMATION_MESSAGE); } else{ JOptionPane.showMessageDialog(null, "繼續努力,謝謝", "提示", JOptionPane.INFORMATION_MESSAGE); } break; case 7: case 8: if(num4>0){ jf.jguageNum.setText(Integer.toString(fenshu + 20 * num4)); JOptionPane.showMessageDialog(null, "恭喜,您運氣真好!", "提示", JOptionPane.INFORMATION_MESSAGE); }else{ JOptionPane.showMessageDialog(null, "繼續努力,謝謝", "提示", JOptionPane.INFORMATION_MESSAGE); } break; case 19: case 20: if(num3>0){ jf.jguageNum.setText(Integer.toString(fenshu + 20 * num3)); JOptionPane.showMessageDialog(null, "恭喜,您運氣真好!", "提示", JOptionPane.INFORMATION_MESSAGE); }else{ JOptionPane.showMessageDialog(null, "繼續努力,謝謝", "提示", JOptionPane.INFORMATION_MESSAGE); } break; case 14: if(num2>0){ jf.jguageNum.setText(Integer.toString(fenshu + 20 * num2)); JOptionPane.showMessageDialog(null, "恭喜,您運氣真好!", "提示", JOptionPane.INFORMATION_MESSAGE); }else{ JOptionPane.showMessageDialog(null, "繼續努力,謝謝", "提示", JOptionPane.INFORMATION_MESSAGE); } break; case 15: if(num2>0){ jf.jguageNum.setText(Integer.toString(fenshu + 40 * num2)); JOptionPane.showMessageDialog(null, "恭喜,您運氣真好!", "提示", JOptionPane.INFORMATION_MESSAGE); }else{ JOptionPane.showMessageDialog(null, "繼續努力,謝謝", "提示", JOptionPane.INFORMATION_MESSAGE); } break; case 2: if(num1>0){ jf.jguageNum.setText(Integer.toString(fenshu + 50 * num1)); JOptionPane.showMessageDialog(null, "恭喜,您運氣真好!", "提示", JOptionPane.INFORMATION_MESSAGE); }else{ JOptionPane.showMessageDialog(null, "繼續努力,謝謝", "提示", JOptionPane.INFORMATION_MESSAGE); } break; case 3: if(num1>0){ jf.jguageNum.setText(Integer.toString(fenshu+100*num1)); JOptionPane.showMessageDialog(null, "恭喜,您運氣真好!", "提示", JOptionPane.INFORMATION_MESSAGE); }else{ JOptionPane.showMessageDialog(null, "繼續努力,謝謝", "提示", JOptionPane.INFORMATION_MESSAGE); } break; case 4: if(num1>0){ jf.jguageNum.setText(Integer.toString(fenshu+25*num1)); JOptionPane.showMessageDialog(null, "恭喜,您運氣真好!", "提示", JOptionPane.INFORMATION_MESSAGE); }else{ JOptionPane.showMessageDialog(null, "繼續努力,謝謝", "提示", JOptionPane.INFORMATION_MESSAGE); } break; default: JOptionPane.showMessageDialog(null, "繼續努力,謝謝", "提示", JOptionPane.INFORMATION_MESSAGE); } this.jf.jLabel36.setText("0"); this.jf.jLabel37.setText("0"); this.jf.jLabel38.setText("0"); this.jf.jLabel39.setText("0"); this.jf.jLabel48.setText("0"); this.jf.jLabel49.setText("0"); this.jf.jLabel50.setText("0"); this.jf.jLabel51.setText("0"); this.jbu.setEnabled(true); } }
NetBeans數據庫筆記---三層架構