User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JSP section within the Web Development category of DaniWeb, a massive community of 426,174 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,899 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JSP advertiser: Lunarpages JSP Web Hosting
Views: 4204 | Replies: 9
Reply
Join Date: Mar 2007
Location: UAE
Posts: 6
Reputation: Pinky.Rozita is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Pinky.Rozita's Avatar
Pinky.Rozita Pinky.Rozita is offline Offline
Newbie Poster

Help JSP using Oracle Database

  #1  
Mar 14th, 2007
Hi all ..

this jsp code when we use database access, to get the information by entering the price , my problem is that now iam using oracle as database and i dont know what are the changes that can be made here only ofcorse the path of database this i have , but string will it change because in oracle its varchar ? .. !! .. plz help.. its my first time to do this ..

Thanks

<html>
<body>

<%@ page import="java.sql.*" %>
<%
Connection con = null;
Statement st;
ResultSet rs;
String sql = "";

int price;

price= Integer.parseInt(request.getParameter("price"));

sql = "SELECT * FROM itemTBL where price = "+ price;



try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:prodDSN");
st = con.createStatement();
rs = st.executeQuery(sql);
%>
<table border=2>
<%
while(rs.next())
{
%> <tr>
<td><%=rs.getString("prodno")%></td>
<td><%=rs.getString("prodname")%></td>
<td><%=rs.getString("price")%></td>
<td><%=rs.getString("ptype")%></td>
</tr>
<% } %>
</table>
<%
rs.close();
con.close();

}

catch(Exception ex)
{
out.print(ex.getMessage());
}

%>
</body>
</html>
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2006
Posts: 1,460
Reputation: masijade is just really nice masijade is just really nice masijade is just really nice masijade is just really nice masijade is just really nice 
Rep Power: 9
Solved Threads: 131
masijade's Avatar
masijade masijade is online now Online
Nearly a Posting Virtuoso

Re: JSP using Oracle Database

  #2  
Mar 14th, 2007
No. The query and setString statements can remain the same. It is, however, not recommended to use scriptlets this way. The DB code should be moved out to a bean. Scriptlets, AFAIK, are only still supported for backwards compatability reasons.

You don't want to be backwards do you? ;-)
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote  
Join Date: Mar 2007
Location: UAE
Posts: 6
Reputation: Pinky.Rozita is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Pinky.Rozita's Avatar
Pinky.Rozita Pinky.Rozita is offline Offline
Newbie Poster

Re: JSP using Oracle Database

  #3  
Mar 14th, 2007
Thanks masijade ...

hehe no i dont want .. but this what they taught us in college !! ... basic things ..

however the most important thing is that the code doesnt fully change ... we have 4 weeks to finish the project ..

thanks again
Reply With Quote  
Join Date: Nov 2004
Location: Netherlands
Posts: 5,752
Reputation: jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough 
Rep Power: 18
Solved Threads: 199
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: JSP using Oracle Database

  #4  
Mar 14th, 2007
tough luck. If they teach you to do it like that ask for your money back.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote  
Join Date: Mar 2007
Location: UAE
Posts: 6
Reputation: Pinky.Rozita is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Pinky.Rozita's Avatar
Pinky.Rozita Pinky.Rozita is offline Offline
Newbie Poster

Re: JSP using Oracle Database

  #5  
Mar 18th, 2007
well jwenting im still in a level they dont teach us advanced things ..

Thanks
Reply With Quote  
Join Date: Nov 2004
Location: Netherlands
Posts: 5,752
Reputation: jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough 
Rep Power: 18
Solved Threads: 199
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: JSP using Oracle Database

  #6  
Mar 18th, 2007
they don't teach you the basics either the way they should.
What they are teaching you is the way things were done nearly 10 years ago which has been recognised as exactly the wrong way to do them for the last 7 or so.
If they insist in teaching it like that the course isn't worth the time and money you invest in it.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote  
Join Date: Mar 2007
Location: UAE
Posts: 6
Reputation: Pinky.Rozita is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Pinky.Rozita's Avatar
Pinky.Rozita Pinky.Rozita is offline Offline
Newbie Poster

Re: JSP using Oracle Database

  #7  
Mar 19th, 2007
Aha, well you know better than me ..
Thanks for your consideration.. i will talk to my supervisor about it ..
Reply With Quote  
Join Date: Oct 2007
Posts: 1
Reputation: awsl is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
awsl awsl is offline Offline
Newbie Poster

Re: JSP using Oracle Database

  #8  
Oct 2nd, 2007
any sample codes on how to do this?
Reply With Quote  
Join Date: Aug 2007
Posts: 74
Reputation: lookof2day is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 9
lookof2day lookof2day is offline Offline
Junior Poster in Training

Solution Re: JSP using Oracle Database

  #9  
Oct 2nd, 2007
Hi,

You are using jdbc-odbc bridge to connect to Oracle db here
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:prodDSN");
st = con.createStatement();

You might have successfully created the odbc data source. But it is the Tomcat runtime that needs to know about the odbc datasource. You'll have a tough time configuring it on Tomcat as you are still a student and migh not have played much with the Tomcat configuration files. I would suggest you to use Pure JDBC driver to connect to Oracle database. You can find the Oracle JDBC driver in the Oracle installation folder under jdbc directory. You can find some zip and jar files with name like oci12.zip oci12.jar, etc (Can't remember the names properly so you can send me the names which you find there if you are not sure which one to use). Copy the oci12.jar to either the lib directory of Tomcat Installation ("C:\Program Files\Apache Software Foundation\Apache Tomcat 6.0\lib") or to your web applications WEB-INF\lib (best practice to copy here) directory. Why we are doing it? The runtime environment of Tomcat i.e., the Tomcat's JVM needs the information at runtime about the Oracle related Jdbc drivers (remember the CLASSPATH setting you might have done earlier so that your program knows at runtime where to find specific classes). If you don't do this Tomcat will throw ClassNotFoundException at runtime.

Now, the JDBC specific parameters are need to be passed while establishing the Connection. So you'll have to make the following code changes
try {
              Class.forName ("oracle.jdbc.driver.OracleDriver");//pure jdbc driver
        } catch (ClassNotFoundException e) {
              e.printStackTrace();
        }

        Connection conn = DriverManager.getConnection
             ("jdbc:oracle:thin:@localhost:1526:orcl", "scott", "tiger");
                             // @machineName:port:SID,   userid,  password

        Statement stmt = conn.createStatement();

Please refer the following FAQ on more information
http://www.orafaq.com/faqjdbc.htm
Reply With Quote  
Join Date: Aug 2007
Posts: 74
Reputation: lookof2day is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 9
lookof2day lookof2day is offline Offline
Junior Poster in Training

Solution Re: JSP using Oracle Database

  #10  
Oct 2nd, 2007
I'm sorry for my earlier reply, I thought you r facing Connectivity issues so please ignore it.

According to ur post, the price field is a VARCHAR in Oracle (Correct me if I'm wrong). In that case, the code will change to

int price;

price= Integer.parseInt(request.getParameter("price"));

sql = "SELECT * FROM itemTBL where price = "+ "'"+price+"'";

You will have to convert price to a String(VARCHAR/CHAR format) for comparison to a String (VARCHAR) in database. To avoid the "'" + field + "'", use PreparedStatements. Or JDBC 2.0 features.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb JSP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the JSP Forum

All times are GMT -4. The time now is 6:15 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC