Java reads and fetches data from MySQL

Source: Internet
Author: User
Tags stmt

 Public StaticString URL =NULL;  Public StaticString username =NULL;  Public StaticString Password =NULL;  Public StaticConnection Conn;  Public StaticStatement stmt;  Public StaticResultSet rs;  Public StaticString FileName =NULL;  Public StaticList lists =NewArrayList ();  Public StaticString PATH = "/dbconfig.properties"; Private StaticProperties Properties; Static {        Try{InputStream is= Dblutils.class. getResourceAsStream (PATH); Properties=NewProperties ();            Properties.load (IS); URL= Properties.getproperty ("Jdbc.url"); Username= Properties.getproperty ("Jdbc.username"); Password= Properties.getproperty ("Jdbc.password"); FileName= Properties.getproperty ("FileName"); System.out.println ("FileName:" +fileName); if(Is! =NULL) Is.close (); } Catch(IOException e) {e.printstacktrace (); }    }     Public voidCloseConnection (Connection conn) {if(Conn! =NULL) {            Try{conn.close (); } Catch(SQLException e) {e.printstacktrace (); }        }    }/*** Query MySQL database data and get content * *@paramSQL*/     Public Static voidQuerydatas (String sql) {List<BnsWord> Listsdatas =NewArraylist<bnsword>(); Bnsword BW=NewBnsword (); //bw.setcontent ("content");        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); ResultSet RS=stmt.executequery (SQL); ResultSetMetaData RSMD=Rs.getmetadata (); intColumnCount =Rsmd.getcolumncount (); //Output Column name             for(inti = 1; I <= ColumnCount; i++) {System.out.print (Rsmd.getcolumnname (i)); System.out.print ("(" + rsmd.getcolumntypename (i) + ")"); System.out.print (" | ");            } System.out.println (); //Output Data             while(Rs.next ()) { for(inti = 1; I <= ColumnCount; i++) {System.out.print (rs.getstring (i)+ " | ");            } System.out.println (); }            //conn.commit ();}Catch(SQLException e)        {E.printstacktrace (); }    }

public static void Main (string[] args) {

String sql = "SELECT * FROM TableName";
Querydatas (SQL);

}

Java reads and fetches data from MySQL

Related Article

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.