This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JSP and the fly likes running sql in jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "running sql in jsp" Watch "running sql in jsp" New topic
Author

running sql in jsp

Micahel Kang
Greenhorn

Joined: Jan 11, 2006
Posts: 6
Hi,
I want to execute some oracle sql statements in my jsp page.
Could you please tell me how to establish a connection with the Oracle database.

Thanks,
Prabs
Rohit Suman
Ranch Hand

Joined: Sep 09, 2006
Posts: 37
<%@ page import="java.sql.*"%>
<%
String driver="oracle.jdbc.driver.OracleDriver";
String dburl="jdbcracle:thin:@localhost:1521radata";
String user="scott";
String pwd="tiger";
Connection conn=null;
try {
DriverManager.registerDriver((Driver)Class.forName(driver).newInstance());
conn = DriverManager.getConnection(dburl,user,pwd);
Statement stmt = conn.createStatement();
ResultSet rset = stmt.executeQuery("select * from tab");
while (rset.next())
{
//fetch the data and display what you want.
}
}
catch (Exception e) {
e.printStackTrace();
}
finally
{
try{stmt.close();conn.close();
}catch(Exception e){e.printStackTrace();}
%>
}
don't forget put the classes12.jar in the lib directory
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

PJ
Welcome to JavaRanch!

We're pleased to have you here with us in the JSP forum, but there
are a few rules that need to be followed, and one is that proper names are
required. Please take a look at the
JavaRanch Naming Policy and
adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

You can change it here


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Micahel Kang
Greenhorn

Joined: Jan 11, 2006
Posts: 6
Thanks a lot for the reply.

I have a problem though.
I have installed Tomcat and have been running an application without any problem. But when I start this simple application it says resource not found.
The path of the jsp page is C:\Tomcat 5.0\webapps\jspbook\ch1\

The error message is
HTTP Status 404 - /jspbook/ch1/first.jsp

--------------------------------------------------------------------------------

type Status report

message /jspbook/ch1/first.jsp

description The requested resource (/jspbook/ch1/first.jsp) is not available.

Could you please explain whatz happening here.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Thanks, Michael.
[ September 12, 2006: Message edited by: Ben Souther ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: running sql in jsp
 
Similar Threads
Hibernate and Stored Procedure
Advice OCA DBA and Developer exam?
SQL Script to delete the records based on length
JSP with Oracle / SQL
Can any one help me how can i connect oracle9i with odbc