Simple JSP JDBC Example
// January 29th, 2008 // 3 Comments » // Technology Bits
I learned to program on Java, but have done little to exercise those skills in the last six years, preferring to leverage PHPs simplicity whenever possible. So... when I had a need to put together a simple JSP script that queried Oracle, I found myself scouring for a truly simple example to start from. Here's the example I put together:
-
<br/><%@ page import="java.sql.*" %><br/><%<br/>try <br/>{<br/> //this is how you might get a POST or GET variable from the request to use<br/> //String user_id = request.getParameter("some_var");</p><p> Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@HOSTNAME:PORT:SID","USERNAME","PASSWORD");<br/> Statement stmt = conn.createStatement();<br/> ResultSet rset = stmt.executeQuery("SELECT sysdate FROM dual'");<br/> if (rset.next()) <br/> {<br/> out.println(rset.getString(1));<br/> } <br/> else <br/> {<br/> out.println("No records found");<br/> }<br/> rset.close();<br/> stmt.close();<br/>} <br/>catch (SQLException e) {<br/> out.println("Exception");<br/>}<br/>%><br/>
Certainly for your own purposes you would need to write a lot more code than this. Yet, if you seek a simple example just to gain your bearings, this should do it.




