public class ConnectionUtil { public static Connection getConnection() throws Exception { Connection con = null; String url = "jdbc:mysql://localhost/SampleDB"; String userName = "root"; String passWord = ""; try { Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection(url, userName, passWord); } catch (Exception e) { e.printStackTrace(); throw new Exception(e); } return con; } public static void close(Connection conn , Statement stmt, ResultSet rs){ try { if ( rs != null ){ rs.close(); } if ( stmt != null ) { stmt.close(); } if ( conn != null ){ conn.close(); } } catch(SQLException e){ } } }