Solved

JSP - query data from the database

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Microsoft is moving in-place eDiscovery & hold from ECP to EOP console under Content Search in Search and Investigation Options.  In this post, I will be showing you how to export emails to a PST file using the Content Search Options.
Article by: Justin
In light of the WannaCry ransomware attack that affected millions of Windows machines, you might wonder if your Mac needs protecting. Yes, it does and here is how to do it.
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

728 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