当前位置 : 主页 > 编程语言 > c++ >

mysql java

来源:互联网 收集:自由互联 发布时间:2021-06-30
Main.java package com.zhongkao;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.InputStream;import java.io.InputStreamReader;import java.sql.Connection;import java.sql.DriverManager;import java
Main.java
package com.zhongkao;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
	public static void main(String[] args) throws Exception {
		Main m = new Main();
		m.process();
	}

	private void process() throws Exception {
		Connection conn = getConnection();
		String sql = "insert into list(code, name, s1, s2, score, type) values(?, ?, ?, ?, ?, ?)";
		
		String code = null;
		String name = null;
		String s1 = null;
		String s2 = null;
		double score = 0d;
		String type = null;
		
		String filename = "/mydata/private/all.csv";
		InputStream is = new FileInputStream(new File(filename));
		BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf-8"));  
        String line = null;
        int index = 0;
        while ((line = reader.readLine()) != null) {
        	//code,name,s1,s2,score
        	if (index == 0) {
        		index++;
        		continue;
        	}
        	String[] arr = line.split("[\\,]{1}");
        	code = arr[0];
        	name = arr[1];
        	s1 = arr[2];
        	s2 = arr[3];
        	score = Double.parseDouble(arr[4]);
        	
        	s2 = s2.replaceAll("(", "(");
        	s2 = s2.replaceAll(")", ")");
        	s2 = s2.replaceAll(" ", "");
        	s2 = s2.replaceAll(" ", "");
        	
        	//市一中(统招)
        	String pattern = "(.*)\\((.*)\\).*";
            Pattern r = Pattern.compile(pattern);
            Matcher m = r.matcher(s2);
            if (m.find( )) {
            	s2 = m.group(1);
            	type = m.group(2);
            }
            //System.out.println(s2 + ":" + type);
        	PreparedStatement ps = conn.prepareStatement(sql);
    		ps.setString(1, code);
    		ps.setString(2, name);
    		ps.setString(3, s1);
    		ps.setString(4, s2);
    		ps.setObject(5, score);
    		ps.setString(6, type);
    		ps.executeUpdate();
    		ps.close();
    		index++;
    		System.out.println(name);
        }
        is.close();
        conn.close();
	}

	private Connection getConnection() throws Exception {
		Class.forName("com.mysql.jdbc.Driver");
		String url = "jdbc:mysql://localhost:3306/zhong?useUnicode=true&characterEncoding=UTF-8";
		Connection conn = DriverManager.getConnection(url, "root", "---------");
		return conn;
	}
}
网友评论