Swing 实现进度条将excel和txt文本上传到mysql数据库(附工程压缩包)
程序员文章站
2022-07-13 16:11:23
...
bean 类 public class ImportBean { private String 导入日期; private String 代码; private String 名称; private String 涨幅率; private String 现价; private String 涨跌; private String 买入价; private String 卖出价; private String 总量; private String 现量; private String 涨速率; private String 换手率; private String 今开; private String 最高; public String get导入日期() { return 导入日期; } public void set导入日期(String 导入日期) { this.导入日期 = 导入日期; } public String get代码() { return 代码; } public void set代码(String 代码) { this.代码 = 代码; } public String get名称() { return 名称; } public void set名称(String 名称) { this.名称 = 名称; } public String get涨幅率() { return 涨幅率; } public void set涨幅率(String 涨幅率) { this.涨幅率 = 涨幅率; } public String get现价() { return 现价; } public void set现价(String 现价) { this.现价 = 现价; } public String get涨跌() { return 涨跌; } public void set涨跌(String 涨跌) { this.涨跌 = 涨跌; } public String get买入价() { return 买入价; } public void set买入价(String 买入价) { this.买入价 = 买入价; } public String get卖出价() { return 卖出价; } public void set卖出价(String 卖出价) { this.卖出价 = 卖出价; } public String get总量() { return 总量; } public void set总量(String 总量) { this.总量 = 总量; } public String get现量() { return 现量; } public void set现量(String 现量) { this.现量 = 现量; } public String get涨速率() { return 涨速率; } public void set涨速率(String 涨速率) { this.涨速率 = 涨速率; } public String get换手率() { return 换手率; } public void set换手率(String 换手率) { this.换手率 = 换手率; } public String get今开() { return 今开; } public void set今开(String 今开) { this.今开 = 今开; } public String get最高() { return 最高; //数据库连接 import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.text.SimpleDateFormat; import java.util.Date; import com.mysql.jdbc.Connection; import com.mysql.jdbc.PreparedStatement; public class BaseConnection { Connection conn=null; PreparedStatement ps; ResultSet rsResultSet; public BaseConnection(){ try { Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/gopiao?useUnicode=true&characterEncoding=gbk"; String usern="root"; String pass="root"; conn=(Connection) DriverManager.getConnection(url,usern,pass); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } /** * 将excel文件插入到数据库中 * @return */ public boolean insertExlFile(String 代码,String 名称,String 买入价,String 现价,String 涨幅率,String 涨跌,String 最高,String 总量,String 现量,String 涨速率,String 换手率,String 今开,String 卖出价){ boolean flag=false; SimpleDateFormat sdf=new SimpleDateFormat("HH:ss"); String dateString=sdf.format(new Date()); try { ps=(PreparedStatement) conn.prepareStatement("insert into import_stock(导入日期,代码,名称,买入价,现价,涨幅率,涨跌,最高,总量,现量,涨速率,换手率,今开,卖出价)" + "values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)"); ps.setString(1, dateString); ps.setString(2, 代码); ps.setString(3, 名称); ps.setString(4, 买入价); ps.setString(5, 现价); ps.setString(6, 涨幅率); ps.setString(7, 涨跌); ps.setString(8, 最高); ps.setString(9, 总量); ps.setString(10, 现量); ps.setString(11, 涨速率); ps.setString(12, 换手率); ps.setString(13, 今开); ps.setString(14, 卖出价); int i=ps.executeUpdate(); // flag=ps.execute(); if (i>0) { flag=true; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return flag; } public boolean insertTxtFile(String 名称,String 买入价,String 现价,String 涨幅率,String 涨跌,String 最高,String 总量,String 现量,String 涨速率,String 换手率,String 今开,String 卖出价){ boolean flag=false; SimpleDateFormat sdf=new SimpleDateFormat("HH:ss"); String dateString=sdf.format(new Date()); try { ps=(PreparedStatement) conn.prepareStatement("insert into import_stock(导入日期,名称,买入价,现价,涨幅率,涨跌,最高,总量,现量,涨速率,换手率,今开,卖出价)" + "values(?,?,?,?,?,?,?,?,?,?,?,?,?)"); ps.setString(1, dateString); ps.setString(2, 名称); ps.setString(3, 买入价); ps.setString(4, 现价); ps.setString(5, 涨幅率); ps.setString(6, 涨跌); ps.setString(7, 最高); ps.setString(8, 总量); ps.setString(9, 现量); ps.setString(10, 涨速率); ps.setString(11, 换手率); ps.setString(12, 今开); ps.setString(13, 卖出价); int i=ps.executeUpdate(); if (i>0) { flag=true; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return flag; } /** * 将文件名称存入数据库 * @return */ public boolean insFileName(String fileName){ boolean flag=false; try { ps=(PreparedStatement) conn.prepareStatement("insert into savefile(name) values(?)"); ps.setString(1, fileName); int f=ps.executeUpdate(); if (f>0) { flag=true; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return flag; } /** * 查询文件名称 * @param fileName * @return */ public boolean selFileName(String fileName){ boolean flag=false; try { ps=(PreparedStatement) conn.prepareStatement("select name from savefile where name=?"); ps.setString(1, fileName); rsResultSet=ps.executeQuery(); if (rsResultSet.next()) { flag=true; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return flag; } } //UI import java.awt.AWTException; import java.awt.Component; import java.awt.Image; import java.awt.MenuItem; import java.awt.PopupMenu; import java.awt.SystemTray; import java.awt.Toolkit; import java.awt.TrayIcon; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.FocusEvent; import java.awt.event.FocusListener; import java.awt.event.MouseEvent; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.text.DateFormat; import java.text.DateFormatSymbols; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Locale; import java.util.Random; import java.util.StringTokenizer; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFormattedTextField; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JProgressBar; import javax.swing.JSpinner; import javax.swing.JTextField; import javax.swing.SpinnerDateModel; import javax.swing.SpinnerListModel; import javax.swing.SwingUtilities; import javax.swing.SwingWorker; import javax.swing.WindowConstants; import javax.swing.border.BevelBorder; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import com.DBAccess.BaseConnection; /** * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI * Builder, which is free for non-commercial use. If Jigloo is being used * commercially (ie, by a corporation, company or business for any purpose * whatever) then you should purchase a license for each developer using Jigloo. * Please visit www.cloudgarden.com for details. Use of Jigloo implies * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR * ANY CORPORATE OR COMMERCIAL PURPOSE. */ public class ImportUI extends JFrame implements ActionListener, PropertyChangeListener { /** * */ private static final long serialVersionUID = 1L; private JLabel jLabel1; private JTextField jTextField1; private JButton jButton1; JFileChooser jfc = new JFileChooser(); private JLabel jLabel5; private JLabel jLabel4; private JLabel jLabel3; private JSpinner jSpinner2; private JSpinner jSpinner1; private JProgressBar progressBar; private JLabel jLabel2; private JButton jButton2; public static String FILEPATH; public static String FILENAME; private Task task; private TxtTask txtTask; private TrayIcon trayIcon = null; private Object jspinnerDate; public boolean isTrue; protected Component ui; private FocusListener focusListener; public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { ImportUI inst = new ImportUI(); inst.setLocationRelativeTo(null); inst.setVisible(true); } }); } public ImportUI() { super("请选择"); initGUI(); jfc.setCurrentDirectory(new File("d:\\")); this.setResizable(false); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } @SuppressWarnings("unchecked") private void initGUI() { if (SystemTray.isSupported()) { // 检查当前系统是否支持系统托盘 SystemTray tray = SystemTray.getSystemTray();// 获取表示桌面托盘区的 Image image = Toolkit.getDefaultToolkit().getImage("D:\\image.jpg"); PopupMenu popupMenu = new PopupMenu(); MenuItem exitItem = new MenuItem("exit"); exitItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { System.exit(0); } catch (Exception ex) { ex.printStackTrace(); } } }); popupMenu.add(exitItem); trayIcon = new TrayIcon(image, "rjl 导入托盘", popupMenu); trayIcon.addMouseListener(new java.awt.event.MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { if (e.getClickCount() == 2) { showIT(true); } } }); try { tray.add(trayIcon); // 将 TrayIcon 添加到 SystemTray。 } catch (AWTException e) { System.err.println(e); } } else { System.out.println("你的系统不支持系统托盘"); } List list = new ArrayList(); List list2 = new ArrayList(); for (int i = 1; i 0 || f.getName().indexOf("xls") > 0) { progressBar.setIndeterminate(true); task = new Task(); task.addPropertyChangeListener(this); task.execute(); // this.testExcelUpload(); } else if (f.getName().indexOf("TXT") > 0 || f.getName().indexOf("txt") > 0) { // textTxtUpload2(); progressBar.setIndeterminate(true); txtTask = new TxtTask(); txtTask.addPropertyChangeListener(this); txtTask.execute(); } } /** * 判断单元格数据类型 * * @param cell * @return */ public static String getCellValue(HSSFCell cell) { Object res = null; if (cell.getCellType() == 0) { res = cell.getNumericCellValue(); } else { res = cell.getStringCellValue(); } if (res != null) { return res.toString(); } return null; } /** * 导入excel * * @author rjl * */ class Task extends SwingWorker { /* * Main task. Executed in background thread. */ @Override public Void doInBackground() { Random random = new Random(); int progress = 0; setProgress(0); boolean flag = false; jLabel5.setText("正在上传..."); BaseConnection bConnection = new BaseConnection(); try { // 创建对Excel工作簿文件的引用 HSSFWorkbook wookbook = new HSSFWorkbook(new FileInputStream( FILEPATH)); // 在Excel文档中,第一张工作表的缺省索引是0 // 其语句为:HSSFSheet sheet = workbook.getSheetAt(0); HSSFSheet sheet = wookbook.getSheetAt(0); // 获取到Excel文件中的所有行数 int rows = sheet.getPhysicalNumberOfRows(); System.out.println(rows + "uuuuuuuuuuuuuuu"); // 遍历行 for (int i = 1; i = 2) { // val[j] = val[j]; // } else if (count 0) { // String string = val[j].trim(); // double str = new Double(string // .substring(0, // string.length() - 1)); // str = str * 10000; // val[j] = String.valueOf(str); // } // } else { // val[j] = val[j]; // } } } flag = bConnection.insertExlFile(val[0], val[1], val[2], val[3], val[4], val[5], val[6], val[7], val[8], val[9], val[10], val[11], val[12]); } progress = i * 100 / rows; setProgress(progress); } if (flag) { bConnection.insFileName(FILENAME); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } /* * Executed in event dispatch thread */ public void done() { jLabel5.setText(""); Toolkit.getDefaultToolkit().beep(); progressBar.setValue(100); JOptionPane.showMessageDialog(progressBar, "导入成功!"); jSpinner1.setEnabled(false); progressBar.setValue(0); } } /** * 导入txt文本 * * @author rjl * */ class TxtTask extends SwingWorker { @Override protected Void doInBackground() throws Exception { jLabel5.setText("正在上传..."); Random random = new Random(); int progress = 0; setProgress(0); boolean flag = false; BaseConnection bConnection = new BaseConnection(); String 总量; String 现量; try { BufferedReader buffin = new BufferedReader( new InputStreamReader(new FileInputStream(FILEPATH), "gbk")); String b[] = new String[100]; int i = 0; List datas = new ArrayList(); try { String temp = null; while ((temp = buffin.readLine()) != null) { datas.add(temp); // System.out.println(datas.get(i)); i++; } for (int j = 1; j 0) { // String string = b[k - 5].trim(); // double str = new Double(string.substring(0, // string.length() - 1)); // str = str * 10000; // System.out.println(b[k-5]+"***********************"); // b[k-5]=b[k-5]+"lll"; // 总量 = String.valueOf(str); // } else { 总量 = b[k - 5]; // } // if (b[k - 4].indexOf("万") > 0) { // String string = b[k - 4].trim(); // double str = new Double(string.substring(0, // string.length() - 1)); // str = str * 10000; // 现量 = String.valueOf(str); // } else { 现量 = b[k - 4]; // } String 涨速率 = b[k - 3]; String 换手率 = b[k - 2]; String 今开 = b[k - 1]; String 卖出价 = b[k]; flag = bConnection.insertTxtFile(名称, 买入价, 现价, 涨幅率, 涨跌, 最高, 总量, 现量, 涨速率, 换手率, 今开, 卖出价); } k++; progress = i * 100 / i; setProgress(progress); } } if (flag) { // JOptionPane.showMessageDialog(this, "导入成功!"); bConnection.insFileName(FILENAME); } } catch (IOException e) { e.printStackTrace(); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } return null; } public void done() { jLabel5.setText(""); Toolkit.getDefaultToolkit().beep(); progressBar.setValue(100); JOptionPane.showMessageDialog(progressBar, "导入成功!"); jSpinner1.setEnabled(false); progressBar.setValue(0); } } @Override public void propertyChange(PropertyChangeEvent evt) { if ("progress" == evt.getPropertyName()) { int progress = (Integer) evt.getNewValue(); progressBar.setIndeterminate(false); progressBar.setValue(progress); } } } } public void set最高(String 最高) { this.最高 = 最高; } } //sql create database gopiao; create table import_stock( id int auto_increment primary key, 导入日期 varchar(19) NOT NULL, 代码 varchar(10) default NULL, 名称 varchar(30) NOT NULL, 买入价 varchar(8) default NULL, 现价 varchar(8) default NULL, 涨幅率 varchar(8) default NULL, 涨跌 varchar(8) default NULL, 最高 varchar(8) default NULL, 总量 varchar(15) default NULL, 现量 varchar(15) default NULL, 涨速率 varchar(8) default NULL, 换手率 varchar(8) default NULL, 今开 varchar(8) default NULL, 卖出价 varchar(8) default NULL ) create table savefile(id int auto_increment primary key,name varcahr(255))