I have a .jsp file which basically displays data from a database. The database class is defined separately in a .java file. To get the contents of the database I am calling the getData method of the database. But the function calls made never execute and nothing is returned.
However if I return any pre-computed values from the getData function, then it executes fine.
I want to know how can I access the database from the .jsp file.
I don't want to add the java code directly to the .jsp file. So I want to do it via a method call.
Function from .jsp file:
<%
ArrayList<String> al = com.Database.getData();
%>
Java function:
getData(){
al = new ArrayList<String>();
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
al.add("first");
try{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL,USER,PASS);
stmt = conn.createStatement();
String sql = "SELECT rs FROM DATABASE";
rs = stmt.executeQuery(sql);
while(rs.next()){
String str = rs.getString("str");
al.add(str);
}
}catch(Exception e){
e.printStackTrace();
}
al.add("nikunj ");
al.add("banka ");
return al;
}
The contents of the arraylist after the call are {"first", "nikunj", "banka"} and no data from the database.
How can I get the data from the database. I have tried creating a static block that will populate the ArrayList at the start of the program but even this is not working.