empacotar cn.itcast.day04.test; importação java.io. * ; importação java.sql.Connection; importação java.sql.DriverManager; importação java.sql.PreparedStatement; importação java.sql.SQLException; importação java.util.ArrayList; importação java.util.Arrays; importação java.util.List; importação java.util.regex.Matcher; importação java.util.regex.Pattern; público classe ConMysql { pública estáticos vazios principais (String [] args) { String [] [] cordas= WriteToDat ( "E: \\ \\ Java básico-código \\ day04-src código \\ \\ \\ cn itcast \\ day04 \\ \\ de teste en_ch.js" ); // getConnection ( "jdbc: mysql: // localhost: 3306", "db1", "root", "root"); insertIntoTable ( "jdbc: mysql: // localhost: 3306", "db1", "root", "root", "user" , cordas); } Private static string url_unic = "useUnicode = true & characterEncoding = utf8?" ; // 读文件 público estático String [] [] writeToDat (path String) { arquivo Arquivo = novo arquivo (caminho); Lista <String> String [] [] cordas = nulo ; tente { InputStreamReader ISR = new InputStreamReader ( novo FileInputStream (arquivo), "utf8" ); BufferedReader bw = novo BufferedReader (ISR); String line = nulo ; enquanto ((linha = bw.readLine ()) =! nula ) { se (line.equals ( ""! {)) list.add (linha); } } Bw.close (); } captura(IOException e) { e.printStackTrace (); } Lista ); = list.subList (1, list.size () - 1 ); cordas = novo String [list.size ()] [2 ]; // System.out.println (lista); para ( int a = 0, um <list.size (); um ++ ) { String [] dividida = list.get (a) .Split ( ":" ); // System.out.println (Arrays.toString (split)); para ( int b = 0; b <split.length; b ++ ) { padr P = Pattern.compile ( "| \\ s * | \ t | \ r | \ n" de Correspondência de m = p.matcher (split [b]); divisão [b] = m.replaceAll ( "" ); se (! a = list.size () - 1 ) { se (b == split.length - 1 ) { string substring = split [b] .substring (0 , dividir [b] .length ()); cordas [a] [b] = subsequência; } Else { cordas [a] [b] = divisão [b]; } } Else { cordas [a] [b]= dividida [b]; } } } // para (int i = 0; i <list.size (); i ++) { // String [] r = list.get (i) .Split ( "\ t"); // // para (int j = 0; j <st.length; j ++) { // padr P = Pattern.compile ( "| \\ s * | \ t | \ r | \ n"); // Matcher m = p.matcher (r [j]); // r [j] = m.replaceAll ( ""); // r [j] = r [j] .replaceAll ( ":" ""); // cordas [i] [j] = r [j]; // // System.out.printlnl (cadeias [i] [j]); } // [[ '11', 'um'], [ '22', 'two']] System.out.println ( "sucesso dados de carga!" ); voltar cordas; } // 连接数据库 pública estática Connection getConnection (String url, String database, user String, String pwd) { Connection conn = nulo ; tente { Class.forName ( "com.mysql.jdbc.Driver" ); Cordas url_total = url + "/" + dataBase + url_unic; // System.out.printlnl (url_total); conn =DriverManager.getConnection (url_total, utilizador, PWD); System.out.println ( "sucesso conexão!" ); } Prendedor (Exception e) { System.out.println ( "conexão falhar!" ); e.printStackTrace (); } Retornar conn; } // 关闭数据库连接 público estático vazio closeCon (Connection con) { se (con =! Nula ) { try { con.close (); } Pegar (exceção e) { e.printStackTrace (); } } } // 插入数据 público estático boolean insertIntoTable (url String, String database, user String, String pwd, mesa String, String [] [] str) { Connection conn = getConnection (url, banco de dados, usuário, pwd); tente { cadeia de SQL = "inserção em" + tabela + "(" + "uid, uname" + ")" + "valores de" + "(?,?)" ; PreparedStatement pstmt = conn.prepareStatement (SQL); para ( int i = 0; i <str.length; i ++ pstmt.setInt (1, Integer.parseInt (str [i] [0 ])); // System.out.printlnl (str [i] [0]); para ( int j = 1; j <str [i] .length; j ++ ) { // System.out.printlnl (str [i] [j]); pstmt.setString (j + 1 , str [i] [j]); } Pstmt.executeUpdate (); } System.out.println ( "O sucesso de inserção de dados!" ); closeCon (conn); retornar verdadeiro ; } Captura (SQLException e) { e.printStackTrace (); System.out.println ( "inserir dados falhar!" ); closeCon (conn); retornar falsa ; } } }