jdbc 使用sql文件插入数据
- - ITeye博客System.out.println("读取文件内容操作出错");. 已有 0 人发表留言,猛击->> 这里<<-参与讨论. —软件人才免语言低担保 赴美带薪读研.
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class ImportOracle { static class myConnection { private String url = "jdbc:mysql://localhost:3306/jdbc"; private String user = "root"; private String password = ""; // private static JdbcUtilsSing instance = new JdbcUtilsSing(); private static myConnection instance = null; private myConnection() { } public static myConnection getInstance() { if (instance == null) { synchronized (ImportOracle.class) { if (instance == null) { instance = new myConnection(); } } } return instance; } static { try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { throw new ExceptionInInitializerError(e); } } public Connection getConnection() throws SQLException { return DriverManager.getConnection(url, user, password); } public void free(ResultSet rs, Statement st, Connection conn) { try { if (rs != null) rs.close(); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (st != null) st.close(); } catch (SQLException e) { e.printStackTrace(); } finally { if (conn != null) try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } } public String readFile(String filePathAndName) { String fileContent = ""; try { File f = new File(filePathAndName); String charset = getSeaset(filePathAndName); if (f.isFile() && f.exists()) { InputStreamReader read = new InputStreamReader( new FileInputStream(f), charset); BufferedReader reader = new BufferedReader(read); String line; while ((line = reader.readLine()) != null) { fileContent += line + " "; } read.close(); } } catch (Exception e) { System.out.println("读取文件内容操作出错"); e.printStackTrace(); } return fileContent; } private void insert(String datas) throws SQLException{ String[] datass = datas.split(";"); Connection conn = myConnection.getInstance().getConnection(); conn.setAutoCommit(false); Statement stat = conn.createStatement(); int i = 0; for (String string : datass) { stat.addBatch(string); i ++; if(i / 1000 == 0 ||i == datass.length) { stat.executeBatch(); conn.commit(); } } } private String getSeaset(String path) { try { File file = new File(path); InputStream ios = new java.io.FileInputStream(file); byte[] b = new byte[3]; ios.read(b); ios.close(); if (b[0] == -17 && b[1] == -69 && b[2] == -65) return "UTF-8"; else return "GBK"; } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return "GBK"; } public static void main(String[] args) throws SQLException { ImportOracle ip = new ImportOracle(); ip.insert(ip.readFile("D:\\123.txt")); } }