Like I said guys, just need to know why it would catch the exception. Calling SQLUtil in the java servlet below causes the exception, but it doesn't happen in any other class I test with.
I'm working on a school project and trying to get an SQL table into a servlet that outputs html (odd, I know. I don't have much of a choice in the matter.) So to do this I used a DAO object to import and translate the information into a list. However, importing that object only seems to work in my standard java classes, not the servlet, which catches an exception. Here's all the code, sorry for any ridiculousness
The DAO, SQLUtil
package sqlUtil;
import java.sql.*;
import java.util.*;
import java.io.*;
import sqlUtil.VideoRecording;
public class SQLUtil {
private Connection myConn;
public SQLUtil() throws Exception
{
// get db properties
Properties props = new Properties();
props.load(new FileInputStream("proj.properties"));
String user = props.getProperty("user");
String password = props.getProperty("password");
String dburl = props.getProperty("dburl");
// connect to database
myConn = DriverManager.getConnection(dburl, user, password);
}
public List<VideoRecording> getAllVideos() throws Exception
{
List<VideoRecording> list = new ArrayList<>();
Statement myStmt = null;
ResultSet myRs = null;
try
{
myStmt = myConn.createStatement();
myRs = myStmt.executeQuery("select * from Video_Recordings");
while(myRs.next())
{
VideoRecording tempRecording = convertRowToVideoRecording(myRs);
list.add(tempRecording);
}
return list;
}
finally{
close(myStmt,myRs);
}
}
private VideoRecording convertRowToVideoRecording(ResultSet myRs) throws SQLException
{
int recording_id = myRs.getInt("recording_id");
String director = myRs.getString("director");
String title = myRs.getString("title");
String category = myRs.getString("category");
String image_name = myRs.getString("image_name");
int duration = myRs.getInt("duration");
String rating = myRs.getString("rating");
int year_released = myRs.getInt("year_released");
double price = myRs.getDouble("price");
int stock_count = myRs.getInt("stock_count");
VideoRecording tempVideo = new VideoRecording(recording_id, director, title, category, image_name, duration, rating, year_released, price, stock_count);
return tempVideo;
}
private static void close(Connection myConn, Statement myStmt, ResultSet myRs)
throws SQLException {
if (myRs != null) {
myRs.close();
}
if (myStmt != null) {
}
if (myConn != null) {
myConn.close();
}
}
private void close(Statement myStmt, ResultSet myRs) throws SQLException {
close(null, myStmt, myRs);
}
}
And the class it works in:
package sqlUtil;
import java.util.*;
public class TestUtil {
/**
* @param args
*/
public static void main(String[] args)
{
try{
VideoRecording test = new VideoRecording(1, "directordave", "titletest","categorytest","imagenametest",12,"Ratingtest",1995,21.2,21);
System.out.println(test.getCategory());
SQLUtil util = new SQLUtil();
List<VideoRecording> testList = util.getAllVideos();
System.out.println(testList.size());
System.out.println(testList.get(0).getId());
}
catch(Exception exc)
{
System.out.println("failure");
}
}
}
And the servlet class it doesn't work in:
@WebServlet("/videos")
public class VideoListing extends HttpServlet {
private static final long serialVersionUID = 1L;
public VideoListing()
{
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
response.setBufferSize(1024);
PrintWriter out = response.getWriter();
try {
SQLUtil util = new SQLUtil();
List<VideoRecording> testList = util.getAllVideos();
System.out.println(testList.size());
System.out.println(testList.get(0).getId());
out.println("<html><body>");
out.println( "<table border='1' style='width=100%'><tr>");
out.println("<td>Recording ID</td>");
out.println("<td>Director</td>");
out.println("<td>Title</td>");
out.println("<td>Category</td>");
out.println("<td>Duration</td>");
out.println("<td>Rating</td>");
out.println("<td>Year Released</td>");
out.println("<td>Price</td>");
out.println("<td>Stock Count</td>");
//For (every employee in list) print each of those as a data object in the table
out.println("<tr>");
//for(int i=0; i<testList.size(); i++)
//{
// out.println("<td>"+testList.get(i).getId()+"</td>");
//}
out.println("</tr>");
out.println("</tr></table>");
out.println( "</body></html>");
}
catch (Exception exc)
{
out.println("complete failure");
}
}
}
Thanks for any help ahead of time.
I think this is the stacktrace. It's saying the file isn't found for the properties file, but why would that only happen in the servlet, not the java class? They're all in the same project, and the properties file IS there.
java.io.FileNotFoundException: proj.properties (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:146)
at java.io.FileInputStream.<init>(FileInputStream.java:101)
at sqlUtil.SQLUtil.<init>(SQLUtil.java:14)
at servlets.VideoListing.doGet(VideoListing.java:34)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)