?
Solved

JSP - query data from the database

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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

I came across an unsolved Outlook issue and here is my solution.
Social messanging services like WhatsApp and Facebook can help businesses in ways that many owners don't even imagine, giving new opportunities to connect with customers. Discover some of the most innovative things they can do for your company.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month14 days, 15 hours left to enroll

839 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