MySQL implementation batch import, and solve Chinese garbled problem

Source: Internet
Author: User
Tags stmt

 Public StaticString url = "Jdbc:mysql://ip/database?characterencoding=utf-8"; Add a. Characterencoding=utf-8 on the database to solve the problem of Java inserting data garbled in Chinese Public StaticString username = "User";  Public StaticString Password = "123";  Public StaticConnection Conn;  Public StaticStatement stmt;  Public StaticResultSet rs;  Public StaticDatastructure ds =Newdatastructure (); /*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 voidCloseConnection (Connection conn) {if(Conn! =NULL) {            Try{conn.close (); } Catch(SQLException e) {e.printstacktrace (); }        }    }/*//Implement query operation public static void Select (String sql) {try {rs = stmt.executequery (SQL); ResultSetMetaData meta_data = Rs.getmetadata ();//column name for (int i_col = 1; i_col <= Meta_data.getcolumncou NT ();            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 ("Data query failed!"); }    }*/     Public Static voidINSERTD (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=NewStringBuilder (); InputStream is=NewBytearrayinputstream (Sb.tostring (). GetBytes ());            ((com.mysql.jdbc.Statement) stmt). Setlocalinfileinputstream (IS);            Stmt.executeupdate (SQL);        Conn.commit (); } Catch(SQLException e) {e.printstacktrace (); }    }     Public Static voidMain (string[] args) {String SQL= ("INSERT into Bns_user (Userurl, Titleurl, Createtime, username, titleabout) VALUES (' 12 ', ' 1 ', ' 1 ', ' 1 ', ' 1 ')");            INSERTD (SQL); }

MySQL implementation batch import, and solve Chinese garbled problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.