?
Solved

JSP - query data from the database

Posted on 2006-11-28
4
Medium Priority
?
236 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 1200 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 300 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

Geo-targeting is the practice of distributing content based on a person’s location, as best as you can determine it. Let’s look at some ways you could successfully use this tactic. The following tips and case studies could lead to meaningful results.
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

649 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