Solved

JSP - query data from the database

Posted on 2006-11-28
4
225 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
  • 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Learn to move / copy / export exchange contacts to iPhone without using any software. Also see the issues in configuration of exchange with iPhone to migrate contacts.
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

708 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now