Solved

JSP - query data from the database

Posted on 2006-11-28
4
233 Views
Last Modified: 2010-04-01
I have defined the datasouce in web.xml like below

      <resource-ref>
            <res-ref-name>jdbc/SR_DATASOURCE</res-ref-name>
            <res-type>javax.sql.DataSource</res-type>
            <res-auth>Container</res-auth>
      </resource-ref>


I want to know how to i query the database table (tmp_account) from the JSP..
The table only have 3 columns: account_no, account_name, total_amount

Please advise.

it should be very simple but i dont know.
Please giving me a sample program would be greatly appricated
0
Comment
Question by:LeanMoreTryMore
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 23

Accepted Solution

by:
rama_krishna580 earned 400 total points
ID: 18033395
Hi,

look at this sample code:

<FORM name="mail" METHOD="post">
<%@ page import="java.util.*,java.io.*,java.text.*,java.sql.*,javax.naming.*,COM.ibm.db2.jdbc.net.DB2Driver" %>

<%

java.util.Properties parms = new java.util.Properties();

// I tried with com.ibm.websphere.naming.WsnInitialContextFactory, change to the factory that u r using....
parms.setProperty (Context.INITIAL_CONTEXT_FACTORY, "com.ibm.ejs.ns.jndi.CNInitialContextFactory");
javax.naming.Context ctx = new javax.naming.InitialContext(parms);

// I tried java:comp/env/jdbc/Connection. It throwed an error Name comp/env/jdbc not found in context "java:"

javax.sql.DataSource ds = (javax.sql.DataSource) (ctx.lookup("jdbc/SR_DATASOURCE"));
java.sql.Connection conn = ds.getConnection();
java.sql.Statement stmt = conn.createStatement();
java.sql.ResultSet r = stmt.executeQuery("SELECT * FROM tmp_account");
while (r.next()){
String a=r.getString("account_no");
..... take remaining parameters also....

%>

<table>
<tr valign=top align=left bgcolor=#ffffff>
<td align="left" width="20%"><font class="fntreR">Additional Documentation</font></td>
<td align="center" valign="center"><INPUT type="text" name="adddoc" value="<%=a%>" size="70" maxlength="80"></td>
</tr>

</table>
</form>
<%
}
stmt.close();
conn.close();
%>

R.K
0
 
LVL 23

Expert Comment

by:rama_krishna580
ID: 18033414
0
 

Author Comment

by:LeanMoreTryMore
ID: 18033422
I use the tomcat example but not working at SAP AS. it works for Sun AS and Oracle AS

0
 
LVL 10

Assisted Solution

by:jaggernat
jaggernat earned 100 total points
ID: 18033507
make use of opensource jstl in your jsp. follow this example

http://www-128.ibm.com/developerworks/library/j-jstlsql/index.html
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Problem with Pie chart in Jsp 20 235
EJB MDB example 4 89
eclipse luna javeEE perspecive missing 5 119
Unhandled exception type Exception 18 52
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question