mysql 實現大量匯入,並解決中文亂碼問題

來源:互聯網
上載者:User

標籤:

public static String url = "jdbc:mysql://ip/database?characterEncoding=UTF-8";  //在database 後面加上?characterEncoding=UTF-8 就可以解決java 插入資料中文亂碼問題    public static String username = "user";    public static String password = "123";    public static Connection conn;    public static Statement stmt;    public static ResultSet rs;    public static DataStructure ds = new DataStructure();    /*public Connection getConnection() {        try {            Class.forName("com.mysql.jdbc.Driver");        } catch (ClassNotFoundException e1) {            e1.printStackTrace();        }        try {            conn = DriverManager                    .getConnection("jdbc:mysql://ip/databases?user=user&password=pass&useUnicode=true&characterEncoding=utf-8");            conn = DriverManager.getConnection(url, username, password);        } catch (Exception e) {            e.printStackTrace();        }        return conn;    }*/    public void closeConnection(Connection conn) {        if (conn != null) {            try {                conn.close();            } catch (SQLException e) {                e.printStackTrace();            }        }    }/*    // 實現查詢操作    public static void select(String sql) {        try {            rs = stmt.executeQuery(sql);            ResultSetMetaData meta_data = rs.getMetaData();// 列名            for (int i_col = 1; i_col <= meta_data.getColumnCount(); i_col++) {                System.out.print(meta_data.getColumnLabel(i_col) + "   ");            }            System.out.println();            while (rs.next()) {                for (int i_col = 1; i_col <= meta_data.getColumnCount(); i_col++) {                    System.out.print(rs.getString(i_col) + "  ");                }                System.out.println();            }            rs.close();        } catch (Exception e) {            System.out.println("資料查詢失敗!");        }    }*/    public static void insertd(String sql) {        try {            conn = DriverManager.getConnection(url, username, password);            conn.setAutoCommit(false);            stmt = conn.prepareStatement("load data local infile ‘‘ "                    + "into table loadtest fields terminated by ‘,‘");            StringBuilder sb = new StringBuilder();            InputStream is = new ByteArrayInputStream(sb.toString().getBytes());            ((com.mysql.jdbc.Statement) stmt).setLocalInfileInputStream(is);            stmt.executeUpdate(sql);            conn.commit();        } catch (SQLException e) {            e.printStackTrace();        }    }    public static void main(String[] args) {        String sql = ("insert into bns_user (userurl, titleurl, createtime, username, titleabout ) values (‘12‘,‘1‘,‘1‘,‘1‘,‘1‘)");        insertd(sql);            }

 

mysql 實現大量匯入,並解決中文亂碼問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.