首頁 » 原創 » 正文

[原創]Java提取csv到mysql的一種方法

昨天,幾個同學找到我,他們參加某大賽需要將大量數據導入數據庫,而數據存在excel中,我首先想到的是poi接口,後來因為一些原因沒有採用,再後來我們通過另存為csv文件解決了一些問題,但是csv中一個文件有12行每行3個數據,逗號分隔,這樣的錄入量還是很大的,最後我通過編寫可視化的java來解決了這個問題。
package xxx;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.sql.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class xxx extends JFrame {
	public static void main(String[] args) {
		JFrame jf=new JFrame("2016某大賽");
		jf.setBounds(0, 0, 165, 110);
		Container c= new Container();
		JButton jb=new JButton("插入數據");
		JLabel jl=new JLabel("等待操作中...");
		jl.setBounds(0, 50, 150, 25);
		jl.setOpaque(true);
		jl.setBackground(Color.YELLOW);
		jb.setBounds(0, 0, 150, 50);
		c.add(jb);
		c.add(jl);
		jf.add(c);
		jf.setVisible(false);
		jf.setVisible(true);
		jb.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				jl.setText("正在插入,請等待...");
				jl.setBackground(Color.YELLOW);
				buttonInsert();
				jl.setText("插入成功");
				jl.setBackground(Color.GREEN);
				}});
		}
        public static String buttonInsert(){
	String data=null;
	try {
    	String filePath = "C:\\data.csv";
            String encoding="GBK";
            File file=new File(filePath);
            if(file.isFile() && file.exists()){ //判斷文件是否存在
                InputStreamReader read = new InputStreamReader(
                new FileInputStream(file),encoding);//考慮到編碼格式
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt = null;
                int n=1;
                while((lineTxt = bufferedReader.readLine()) != null){
                    //System.out.println(lineTxt);
                	
                	if(n>3){
                	String dataarr[]=new String[12]; 
                	 dataarr=lineTxt.split(",");
         	        insert(dataarr[1],dataarr[2],dataarr[0]);
         	        insert(dataarr[4],dataarr[5],dataarr[3]);
         	        insert(dataarr[7],dataarr[8],dataarr[6]);
         	        insert(dataarr[10],dataarr[11],dataarr[9]);
                	}
                	n++;
             
                }
                read.close();
    }else{
        System.out.println("找不到指定的文件");
    }
    } catch (Exception e2) {
        System.out.println("讀取文件內容出錯");
        e2.printStackTrace();
    }
    System.out.println(data);

//out.flush();
//out.close();
	return null;
}	
		 public static String insert (String power,String speed,String time){
			String result=null;
		    Statement stmt = null;
		    ResultSet rs=null;
		    try { 
				Class.forName("com.mysql.jdbc.Driver");
		        stmt = DriverManager.getConnection(
		                   "jdbc:mysql://localhost:3306/xxx", "root", "12345").createStatement();
		        rs = stmt.executeQuery("SELECT * FROM xxx");
		        System.out.println(rs);
		        ResultSetMetaData rsmd = rs.getMetaData(); 
		        rs.next();
		        stmt.execute("INSERT INTO  `xxx`.`xxx` (`power` ,`speed` ,`ttime` )VALUES ( '"+p1+"','"+p2+"','"+p3+"')");
		        rs.close();
				stmt.close();
		      

		  } catch (Exception e) {
		        e.printStackTrace();
		  }
			return result;
		}
}

本文共 3 個回復

  • 增**購 2016/09/21 17:17

    我對你博客的愛,你永遠不會明白!

  • 卡普利爾·諾 2016/10/05 00:19

    雖然看不懂,但還是看完了(靠着蹩腳的英語看懂了幾個單詞233)(๑•̀ω•́๑)

發表評論