[thelist] Need help building a simple JSP/MySQL webapp

Matt Warden mwarden at gmail.com
Mon Jan 30 22:39:30 CST 2006


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


Rick,

Rick den Haan wrote:
> I've been googling around a bit, but easy-to-understand JSP explanations
> aren't easy to find. Let me start by saying that I'm a PHP programmer, and

I feel your pain.

My first "language" was ASP (so-called Classic ASP). I had to make the
jump to Java on-the-job (this was late in the dot-com boom when lots of
companies were trying anything they could -- like switching to the
language with the greatest hype at the moment -- to stay afloat). And I
found out that there is a lot -- a lot! -- that languages like PHP and
ASP hide from the programmer, which other languages like Java do not.
(And of course there are many other langauges that expose even more.)

> Here's what I have right now (customers.jsp, adapted from an example in the
> book):
> <%
>   //somehow load "com.mysql.jdbc.Driver"
> 
>   Connection jspdb = DriverManager.getConnection
> ("jdbc:mysql://localhost/jsp_test");
>   Statement sql = jspdb.createStatement();
> 
>   out.println("<ol>");
> 
>   ResultSet customers = sql.executeQuery("SELECT * FROM customers ORDER BY
> id ASC");
> 
>   while (customers.next()) {
>     out.println("<li>" + customers.getString(1) + ' - ' +
> customers.getString(2) + ' - ' + customers.getString(3) + '</li>');
>   }
> 
>   out.println('</ol>');
> 
>   jspdb.close();
> %>
> 
> Is someone more used to handling JSP can help me out here, please do.... Be
> gentle, as this is my first time working with this language. I might be
> thinking too much along PHP's mindset here...

It has been a while since I've done Java programming, so the syntax
might be slightly off, but your book can help you there.

The thing that jumps right out at me is that you aren't handling any
exceptions. This can be very Bad(tm), especially when dealing with
things like connections to databases.

If I recall, this is the "proper" way to do what you're trying to do:

Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
	conn = ...;
	stmt = ...;
	rs = ...;
}
catch (SQLException e) {
	e.printStackTrace();
}
finally {
	if (rs != null) {
		try{ rs.close(); }catch(Exception e){}
	}
	if (stmt != null) {
		try{ stmt.close(); }catch(Exception e){}
	}
	if (conn != null) {
		try{ conn.close(); }catch(Exception e){}
	}
}

As for your problem, I can't really help because you didn't say what the
problem is. Are you getting an error? What is it?

- --
Matt Warden
Miami University
Oxford, OH, USA
http://mattwarden.com


This email proudly and graciously contributes to entropy.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFD3uoCrI3LObhzHRMRAkKpAKC5ovoueaTyr5eNk3xijQOILyF2mQCgzEfp
BgqbfblSPPl/uJIMCki/1ck=
=QF7R
-----END PGP SIGNATURE-----



More information about the thelist mailing list