Java generics and JDBC design an interface for U disk information input into the database

1. Experimental content

1. Define a U category, including brand (brand), capacity (size), interface type (type), price (price) and other information. Then write an entry interface for entering U disk information, enter a U disk information, and save it in the database.
2. Read at least 4 pieces of U disk information from the database, and then save these information to the ArrayList collection object, click the "Sort" button to sort the U disk capacity and output.

2. The purpose of the experiment

Master the use of various interfaces and classes in JDBC
Master the design of the database
Familiar with UI interface design

Three, code demonstration

1. U disk information

 package exam; public class Udisk {     private String brand;    private int size;    private String type;    private float price;     public Udisk() {    }     public Udisk(String brand, int size, String type, float price) {        this.brand = brand;        this.size = size;        this.type = type;        this.price = price;    }     public String getBrand() {        return brand;    }     public int getSize() {        return size;    }     public String getType() {        return type;    }     public float getPrice() {        return price;    }     @Override    public String toString() {        return brand + " " + size + " " + type + " " + price;    }}

2. Database help

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */package DB; import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.logging.Level;import java.util.logging.Logger; public class DBHelper {     private static final String URL = "jdbc:mysql://localhost:3306/dvd";//数据库地址    private static final String use = "root";    private static final String passward = "123";    private static Connection conn = null;//先声明一个空的连接对象    private static Statement stat = null;//必须在sql包而不是mysql    //创建数据库连接对象     private static void getConnection() {        try {            Class.forName("com.mysql.jdbc.Driver");            //连接数据库            conn = DriverManager.getConnection(URL, use, passward);            stat = conn.createStatement();//数据库交互对象                   } catch (ClassNotFoundException ex) {            Logger.getLogger(DBHelper.class.getName()).log(Level.SEVERE, null, ex);        } catch (SQLException ex) {            Logger.getLogger(DBHelper.class.getName()).log(Level.SEVERE, null, ex);        }    }     public static ResultSet executeQuery(String sql) {//查询,该参数为sql语句        if (conn == null || stat == null) {            getConnection();        }        ResultSet rs = null;        try {            rs = stat.executeQuery(sql);//交互对象stat把sql语句传入数据库执行返回rs结果集        } catch (SQLException ex) {            Logger.getLogger(DBHelper.class.getName()).log(Level.SEVERE, null, ex);        }        return rs;//返回查询的结果集    }     public static void insert(String sql) {//插入        if (conn == null || stat == null) {            getConnection();        }        try {            stat.executeUpdate(sql);//交互对象stat把sql传给数据库执行        } catch (SQLException ex) {            Logger.getLogger(DBHelper.class.getName()).log(Level.SEVERE, null, ex);        }    }    //关闭数据库     public static void Close() {        try {            if (conn != null && !conn.isClosed()) {                conn.close();                stat = null;            }        } catch (SQLException ex) {            Logger.getLogger(DBHelper.class.getName()).log(Level.SEVERE, null, ex);        }    }}

3. Interface design

package UI; import DB.DBHelper;import exam.Udisk;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.Collections;import java.util.HashSet;import java.util.List;import java.util.Map;import java.util.logging.Level;import java.util.logging.Logger;import java.util.stream.Collectors;import javax.swing.JOptionPane; public class UdiskInput extends javax.swing.JFrame {     public static List<Udisk> list = null;     public UdiskInput() {        initComponents();    } private void jBtnSaveActionPerformed(java.awt.event.ActionEvent evt) {                                                 //把输入的数据赋值给对象ud        Udisk u = new Udisk(String.valueOf(jCmbBrand.getSelectedItem()), Integer.parseInt(jTxtSize.getText()),                String.valueOf(jCmbType.getSelectedItem()), Integer.parseInt(jTxtPrice.getText()));        //把对象数据作为交互对象stat的sql语句的数据插入表中        DBHelper dbh = new DBHelper();        String sql = "insert into UdiskInfo(brand,size,type,price) values('" + u.getBrand()                + "'," + u.getSize() + ",'" + u.getType() + "'," + u.getPrice() + ")";        DBHelper.insert(sql);        JOptionPane.showMessageDialog(this, "数据保存成功");    }                                             private void jBtnReadActionPerformed(java.awt.event.ActionEvent evt) {                                                 // TODO add your handling code here:        list = new ArrayList<Udisk>();        ResultSet rs = DBHelper.executeQuery("select * from UdiskInfo");        int i = 0;        try {            while (rs.next()) {//把所有结果集放入list中                Udisk u = new Udisk(rs.getString("brand"), Integer.parseInt(rs.getString("size")),                        rs.getString("type"), Integer.parseInt(rs.getString("price")));                list.add(u);//保存到集合对象                //这里使用追加文本的方式并换行实现显示所有信息                jTxtAInfo.setText(jTxtAInfo.getText() + list.get(i).toString() + '\n');                i++;            }        } catch (SQLException ex) {            Logger.getLogger(UdiskInput.class.getName()).log(Level.SEVERE, null, ex);        }    }                                             private void jBtnSortActionPerformed(java.awt.event.ActionEvent evt) {                                         //先判断数据量是否大于4        if (list.size() < 4) {            JOptionPane.showMessageDialog(this, "数据量不足无法排序");        } //对U盘容量进行排序然后输出。        //1、把容量参数取出来单独放入一个list集合        //2、使用collections工具类的sort升序排序        //3、获取文本内容再追加排序结果显示信息        List<Integer> SizeList = list.stream().map(Udisk -> Udisk.getSize()).collect(Collectors.toList());        Collections.sort(SizeList);        jTxtAInfo.setText(jTxtAInfo.getText() + '\n' + "容量升序排序:" + '\n' + SizeList.toString());    }                                           // Variables declaration - do not modify                         private javax.swing.JButton jBtnRead;    private javax.swing.JButton jBtnSave;    private javax.swing.JButton jBtnSort;    private javax.swing.JComboBox<String> jCmbBrand;    private javax.swing.JComboBox<String> jCmbType;    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.JScrollPane jScrollPane1;    private javax.swing.JTextArea jTxtAInfo;    private javax.swing.JTextField jTxtPrice;    private javax.swing.JTextField jTxtSize;    // End of variables declaration                   }

Fourth, run screenshots