Initially connection is made for the Database and Query is written for retrieving the Datas.
import java.sql.*; public class RetriveAllEmployees{ public static void main(String[] args) { System.out.println("Getting All Rows from employee table!"); Connection con = null; String url = "jdbc:mysql://localhost:3306/"; String db = "jdbc"; String driver = "com.mysql.jdbc.Driver"; String user = "root"; String pass = "root"; try{ Class.forName(driver); con = DriverManager.getConnection(url+db, user, pass); Statement st = con.createStatement(); ResultSet res = st.executeQuery("SELECT * FROM employee"); System.out.println("Employee Name: " ); while (res.next()) { String employeeName = res.getString("employee_name"); System.out.println(employeeName ); } con.close(); } catch (ClassNotFoundException e) { System.err.println("Could not load JDBC driver"); System.out.println("Exception: " + e); e.printStackTrace(); } catch(SQLException ex) { System.err.println("SQLException information"); while(ex!=null) { System.err.println ("Error msg: " + ex.getMessage()); System.err.println ("SQLSTATE: " + ex.getSQLState()); System.err.println ("Error code: " + ex.getErrorCode()); ex.printStackTrace(); ex = ex.getNextException(); // For drivers that support chained exceptions } } } }