• Post Reply Bookmark Topic Watch Topic
  • New Topic

JDBC and JSP  RSS feed

 
Surya Vamshi
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hI,
cAN ANY BODY TELL ME HOW TO FETCH DATA FROM DATABASE TABLE USING JDBC AND PRESENTING TO JSP.

iS IT OK TO PROVIDE THE JDBC CONNECTION DETAILS IN JSP. ONCE THE CONNECTION HAS BEEN ESTABLISHED HOW TO PRESENT DATA IN JSP
 
Tom Katz
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ONE APPROACH MIGHT BE... err, excuse me - One approach might be to store the Connection object as a Session object, which you can then access in other JSPs/Servlets.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Surya, two things:

1) It is generally considered extremely poor practice to perform DB operations directly within a JSP.

2) It is also considered fairly rude to type in all uppercase. You will find the CAPS LOCK key to the left of your A key. Please use it.

bear
Forum bartender
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tom Katz:
ONE APPROACH MIGHT BE... err, excuse me


Very nice.



One approach might be to store the Connection object as a Session object, which you can then access in other JSPs/Servlets.


Yes why not. But it is extremely EXTREMELY poor practice to place the connection object into session.
 
Rajesh Agarwal
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why dont you use a servlet to do the database operations and then pass the data to the JSP using setAttribute.

Let me know if you understand this.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by an indian:
why dont you use a servlet to do the database operations and then pass the data to the JSP using setAttribute.


Better. But DB connections should not be handled in JSP neither in servlets. Its better to use a plain normal/regular java class for db operations.
 
vikassheel gupta
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Connection con=null;
Statement st=null;
ResultSet rs=null;

try
{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
con=DriverManager.getConnection("");

sql=" ";
st=con.createStatement();
rs=st.executeQuery(sql);

}catch(Exception e)
{
System.out.println("A SQL error is generated :"+e);
}

 
Bryan Scarbrough
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As vikassheel gupta show in the last post it is very easy to create the connection in a class and return results.

As far as displaying it in the JSP the possibilities are almost limitless. What you really have to figure out is do you want the data in a simple <table> format or do you wish to display it in a form type layout. Each has obvious differences and different implementation details. i.e. to use a table you could use a method I will call toHTMLTable like below:


This will take a ResultSet, iterate over each row and append the data onto a StringBuffer. You could then easily use this in a JSP page with the <%= somename.toHTMLTable() %> where the table will be printed to the browser window.

The form on the other hand would be quite different since it would likely requrie only single entries from the query to be displayed in individual text fields. This is also relatively simple with calls like:



Where these will be called in the JSP page with simple:



I hope this helps in some way.
 
ramakrish nan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Surya Vamshi:
hI,
cAN ANY BODY TELL ME HOW TO FETCH DATA FROM DATABASE TABLE USING JDBC AND PRESENTING TO JSP.

iS IT OK TO PROVIDE THE JDBC CONNECTION DETAILS IN JSP. ONCE THE CONNECTION HAS BEEN ESTABLISHED HOW TO PRESENT DATA IN JSP


hear is answer

<%@ page language="java" info="this is answer page" import="java.io.*,java.sql.*" %>
<%
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc dbc:","username","password");
Statment st=con.createStatement();
ResultSet rs=st.executeQuery("select * from emp");
while(rs.next())
{
%>
<li><%=rs.getString(1)%></li>
<%
}
}
catch(Exception g1)
{
out.println("error"+g1);
}


%>
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!