NetBeans数据库笔记

Stella981
• 阅读 679

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);
    }
}
点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
待兔 待兔
2个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Easter79 Easter79
2年前
sql注入
反引号是个比较特别的字符,下面记录下怎么利用0x00SQL注入反引号可利用在分隔符及注释作用,不过使用范围只于表名、数据库名、字段名、起别名这些场景,下面具体说下1)表名payload:select\from\users\whereuser\_id1limit0,1;!(https://o
Java修道之路,问鼎巅峰,我辈代码修仙法力齐天
<center<fontcolor00FF7Fsize5face"黑体"代码尽头谁为峰,一见秃头道成空。</font<center<fontcolor00FF00size5face"黑体"编程修真路破折,一步一劫渡飞升。</font众所周知,编程修真有八大境界:1.Javase练气筑基2.数据库结丹3.web前端元婴4.Jav
Stella981 Stella981
2年前
GreenPlum tidb 性能比较
主要的需求  针对大体量表的OLAP统计查询,需要找到一个稳定,高性能的大数据数据库,具体使用  数据可以实时的写入和查询,并发的tps不是很高建立数据仓库,模式上主要采用星星模型、雪花模型,或者宽表前端展示分为3类 saiku、granafa、c代码开发数据体量:事实表在35亿、维度表大的在500
Stella981 Stella981
2年前
Python3:sqlalchemy对mysql数据库操作,非sql语句
Python3:sqlalchemy对mysql数据库操作,非sql语句python3authorlizmdatetime2018020110:00:00coding:utf8'''
Stella981 Stella981
2年前
Mybatis Plus 更新时间 creatDate字段报错 Could not set property 'creatDate'
背景   MySQL数据库,表中字段名为creatDate,类型为datetime。Java实体类中对应的变量是java.time.LocalDateTime类型的creatDate。当使用新增和更新的方法时,创建日期时间和最后更新时间自动更新。实体类_/\\__\创建日期__\/_@JsonF
Wesley13 Wesley13
2年前
MySQL表的操作01
表在数据库中主要用来实现存储数据记录,其基本操作包括创建表、查看表、删除表和修改表。表中的数据库对象包括:1.列(COLUMNS):也称属性列,在具体创建表时,必须指定列的名字和它的数据类型。2.索引(INDEXES):根据指定的数据库建立起来的顺序,提供了快速访问数据的途径。3.触发器(TRIGGERS):指用户定义的事务命令集合,当对一个
Wesley13 Wesley13
2年前
mysql学习笔记
基础操作创建数据库CREATEDATABASEdb_name;切换数据库USEdb_name;创建表CRETAETABLEtable_name(columntype,...);查看表结构DESCta