Function that Returns No of Rows in ResultSet
public int getRows(ResultSet res) { int totalRows = 0; try { res.last(); totalRows = res.getRow(); res.beforeFirst(); } catch(Exception ex) { return 0; } return totalRows ; }
Function that Returns No of Rows in ResultSet
public int getRows(ResultSet res) { int totalRows = 0; try { res.last(); totalRows = res.getRow(); res.beforeFirst(); } catch(Exception ex) { return 0; } return totalRows ; }
Action Class Code
public class DropDown extends ActionSupport{ private List arrUserDetails = new ArrayList<Users>(); public DropDown() { Users objUsers = new Users(); objUsers.setUserId("101"); objUsers.setUserName("User1"); arrUserDetails.add(objUsers); objUsers.setUserId("102"); objUsers.setUserName("User2"); arrUserDetails.add(objUsers); objUsers.setUserId("103"); objUsers.setUserName("User3"); arrUserDetails.add(objUsers); } public String execute(){ return SUCCESS; } public List getArrUserDetails() { return arrUserDetails; } public void setArrUserDetails(List arrUserDetails) { this.arrUserDetails = arrUserDetails; } }
Users Bean Class
public class Users { private String userId; private String userName; public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } }
JSP Page Struts2 Tag
<s:select label="Departments" list="departmentList" listKey="deptId" listValue="deptName" name="department"/>
This warning is generated since you have specified formName.action in action attribute of form.You get get rid of those by changing the action attribute to formName instead of formName.action
Reason for Error
<s:form action="Reports.action" method="post">
Change it To
<s:form action="Reports" method="post" namespace="/">
Below I have a List of Time to be added together
List<String> arrTime = new ArrayList<String>(); int hour = 0; int minutes = 0; int seconds = 0; String Nmin = null; String Nsec = null; String result = null; arrTime.add("09:05:25"); arrTime.add("09:30:15"); arrTime.add("10:15:01"); arrTime.add("08:19:49"); arrTime.add("09:17:40"); for (Iterator itr = arrTime.iterator(); itr.hasNext();) { String Time = (String) itr.next(); if (Time != null) { String[] rowtime = Time.split(":"); hour += Integer.parseInt(rowtime[0]); minutes += Integer.parseInt(rowtime[1]); seconds += Integer.parseInt(rowtime[2]); } } hour += minutes/60; minutes += seconds/60; minutes %= 60; seconds %= 60; if (minutes < 10) Nmin = "0" + String.valueOf(minutes); else Nmin = String.valueOf(minutes); if (seconds < 10) Nsec = "0" + String.valueOf(seconds); else Nsec = String.valueOf(seconds); result = hour + ":" + Nmin + ":" + Nsec; System.out.println(result);
Output
46:28:10
public class Database { private static DataSource dataSource; static { try { dataSource = new InitialContext().lookup("jndifordbconc"); }catch (NamingException e) { throw new ExceptionInInitializerError("'jndifordbconc' not found in JNDI", e); } } public static Connection getConnection() { return dataSource.getConnection(); } } public List<Entity> list() throws SQLException { Connection connection = null; PreparedStatement statement = null; ResultSet resultSet = null; List<Entity> entities = new ArrayList<Entity>(); try { connection = Database.getConnection(); statement = connection.prepareStatement("SELECT id, foo, bar FROM entity"); resultSet = statement.executeQuery(); while (resultSet.next()) { Entity entity = new Entity(); entity.setId(resultSet.getLong("id")); entity.setFoo(resultSet.getString("foo")); entity.setBar(resultSet.getString("bar")); entities.add(entity); } } finally { if (resultSet != null) try { resultSet.close(); } catch (SQLException ignore) {} if (statement != null) try { statement.close(); } catch (SQLException ignore) {} if (connection != null) try { connection.close(); } catch (SQLException ignore) {} } return entities; }
Time to be Added together
TimeWorked 09:05:25 09:30:15 10:15:01 08:19:49 09:17:40
Code
int hour = 0; int minute = 0; for() { String[] rowtime= row[i].split(":"); hour += Integer.parseInt(rowtime[0]); minute += Integer.parseInt(rowtime[1]); } hour += minute / 60; minute %= 60; String result = hour + ":" + minute if(minute<10) Nmin = "0"+ String.valueOf(minute); else Nmin = String.valueOf(minute); String result = hour + ":" + Nmin;
Calendar cal = Calendar.getInstance(); int daysBackToSat = cal.get(Calendar.DAY_OF_WEEK ); if(daysBackToSat<7) { daysBackToSat += 7; } cal.add(Calendar.DATE, daysBackToSat*-1); System.out.println(sdf.format(cal.getTime()));
import java.util.List; public class ClassObj extends Mac { public static void main(String args[]) { Mac objMac = new Mac(); System.out.println(Mac.class.isAssignableFrom(ClassObj.class)); } } class Mac { }
Output
true
Inheritance refers to using the structure and behavior of a superclass in a subclass. Polymorphism refers to changing the behavior of a superclass in the subclass.v
Inheritance is more a static thing (one class extends another) while polymorphism is a dynamic/ runtime thing (an object behaves according to its dynamic/ runtime type not to its static/ declaration type).
Inheritance is when a ‘class’ derives from an existing ‘class’. So if you have a Person class, then you have a Student class that extends Person, Student inherits all the things that Person has.
Polymorphism deals with how the program decides which methods it should use, depending on what type of thing it has. If you have a Person, which has a read method, and you have a Student which extends Person, which has its own implementation of read, which method gets called is determined for you by the runtime, depending if you have a Person or a Student
Person p = new Student();
p.read();
The read method on Student gets called. Thats the polymorphism in action. You can do that assignment because a Student is a Person, but the runtime is smart enough to know that the actual type of p is Student.
The main difference is polymorphism is a specific result of inheritance. Polymorphism is where the method to be invoked is determined at runtime based on the type of the object. This is a situation that results when you have one class inheriting from another and overriding a particular method. However, in a normal inheritance tree, you don’t have to override any methods and therefore not all method calls have to be polymorphic
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int lngCookieSet = 0; String strCookieVal = ""; String cookieName = "Name"; Cookie[] cookies = request.getCookies(); if(cookies != null && cookies.length > 0) { for(int i=0;i<cookies.length;i++) { if(cookieName.equals(cookies[i].getName())) { lngCookieSet = 1; strCookieVal = cookies[i].getValue(); } } } if(lngCookieSet == 1) { PrintWriter prw = response.getWriter(); prw.print(strCookieVal); } else { Cookie cook = new Cookie("Name", "Mugil"); cook.setMaxAge(24*60*60*365);//1 Year response.addCookie(cook); } }
1.We should check
cookies != null
otherwise the compiler will generate null pointer exception
2.The
new Cookie
constructor will take Cookie Name and Value as Parameter.
3.The addCookie Will add cookie to response Header to set cookie on Client Side