首页 » 原創 » 正文

[原创]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)(๑•̀ω•́๑)

发表评论