/*************************************************************************************** / Read data from Mysql database /**************************************************************************************/ import java.sql.*; public class Java_read { public static void main(String[ ] args) { String host = "jdbc:mysql://localhost/"; //from localhost // String host = "jdbc:mysql://storm.cis.fordham.edu.edu/"; //from remote PC String db = "demo2"; String user = "demo2"; String pswd = "demo2"; Connection connect = null; Statement stmt = null; String sql = null; ResultSet rs = null; try { Class.forName("com.mysql.cj.jdbc.Driver"); //dynamically load //the JDBC Drive class String host_db = host + db; connect = DriverManager.getConnection(host_db, user, pswd); stmt = connect.createStatement( ); //create a statement object sql = "SELECT * FROM cust_order " + "ORDER BY 1 "; rs = stmt.executeQuery(sql); System.out.println("ORDER# \t FNAME \t LNAME \t\t ADDRESS"); //print headers while(rs.next()) { int id = rs.getInt("order_id"); String lname = rs.getString("lastname"); String fname = rs.getString("firstname"); String addr = rs.getString("address"); System.out.printf("%d \t %-7.7s %-15.15s %-16.16s \n", id,fname,lname,addr); } rs.close(); stmt.close(); connect.close(); } catch (Exception e) { System.out.println(e); } } }