public class Database
public void setDriver(String driver)
} // end setDriver
public void setConnect(String uri,String user, String pass)
this.db = Connection db = DriverManager.getConnection(uri, user, pass);
} // end setConnect()
} // end class
Then call it from jsp
<jsp:useBean id="database" class"bean.Database" scope="page"/>
database.setConnect("bla", "user", "pass");
Thanks for the help.
I was wondering if it was possible to create a database conection use Classes in Java and JSP. I do this when I use PHP to prevent me from having to type out the connection strings over and over again. On each page that I use a query.
Example with a class that does some JDBC stuff and a JSP page that calls it.
Are you asking "Instead of doing JDBC operations in a JSP page, can I create a helper class that does some database operations and then call that helper class from JSP?" If so, then the answer is "Yes, of course!" You certainly should not put the JDBC calls directly in your JSP page. Calling a helper class is better. But, there are some even better options than that:
I prefer the second option from this list, but certainly your idea is much better than putting all the JDBC code right in the JSP page where it would be hard to debug, hard to test, and impossible to reuse in other pages.