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