• Post Reply Bookmark Topic Watch Topic
  • New Topic

code for JSP JDBC  RSS feed

 
Anand Gondhiya
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I need some sample code for JSP and JDBC. i.e JDBC code written in java class and we are using that class in our jsp page.
meaning to say , I want to see some samle codes in which presentation and buisness logic are isolated.
Can anybody pls help ?
thanks
-Anand.
 
Gert Cuppens
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using DAO's or Data Access Objects to deal with a MySQL database and JSP's.
In the DAO I write :
(PS the extension of GeneralDao is needed because I only define once which
JDBC driver I'm using to getConnection() )
public class LandDao extends GeneralDao {
///////////////////////////////////////
// operations
/**
* Geeft alle actieve landen weer
*
*
* @return Collection
* @param _ID
*/
public ArrayList zoekLand()
throws SQLException
{
ArrayList landLijst = new ArrayList();
StringBuffer zoekString = new StringBuffer();
zoekString.append("select * from land");
Connection con = null;
/* initialisatie is nodig om compilatie
fout te vermijden */
PreparedStatement ps = null;
ResultSet rs = null ;

try {
System.out.println("opzoeken van alle landen");
con = getConnection();
ps = con.prepareStatement(zoekString.toString());
rs = ps.executeQuery();
while (rs.next())
{
Land land = new Land(rs);
landLijst.add(land);
}
System.out.println("landlijst is opgebouwd");
} catch (SQLException SQLEx)
{
throw SQLEx;
}
finally
{
rs.close();
ps.close();
con.close();
}

return landLijst ;
} // end zoekLand
ANd LandLijst is defined as a Java Bean inside the JSP :
<%@ page import = "org.brukkerlin.adres.Land" %>
<%@ page import = "java.util.Iterator" %>
<jsp:useBean id="servletPath" class="java.lang.String" scope="request"/>
<jsp:useBean id="landLijst" class="java.util.ArrayList" scope="session"/>
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!