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); }}