Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 28-02-2009
Member
 
Join Date: Feb 2009
Posts: 62
How JSP call JDBC database connection

Hello Everybody I wrote a jdbc class to connect database. I have to call this class from jsp. Can I do that. Can anybody tell me the procedure to do that. Thanks in advance
Reply With Quote
  #2  
Old 28-02-2009
Member
 
Join Date: Jan 2009
Posts: 41
Re: How JSP call JDBC database connection

I used the following method to make the connection with the JSP and am calling like this:

java.util.Collection rows = cf.getRowsEx(Integer.parseInt(linkToForm),linkInfo.getDefaultSortCol(), linkInfo.getDefaultSortDir(),searchHash, new java.util.HashMap());
Reply With Quote
  #3  
Old 28-02-2009
Member
 
Join Date: Mar 2008
Posts: 258
Re: How JSP call JDBC database connection

JSP Data-Access Sample Using JDBC

The following example creates a query dynamically from search conditions the user enters through an HTML form (typed into a box, and entered with an Ask Oracle button). To perform the specified query, it uses JDBC code in a method called runQuery() that is defined in a JSP declaration. It also defines a method, formatResult(), within the JSP declaration to produce the output. The runQuery() method uses the scott schema with password tiger.

The HTML INPUT tag specifies that the string entered in the form be named cond. Therefore, cond is also the input parameter to the getParameter() method of the implicit request object for this HTTP request, and the input parameter to the runQuery() method (which puts the cond string into the WHERE clause of the query).

Code:
<%@ page language="java" import="java.sql.*" %>
<HTML>
<HEAD> <TITLE> The JDBCQuery JSP  </TITLE> </HEAD>
<BODY BGCOLOR="white">
<% String searchCondition = request.getParameter("cond"); 
   if (searchCondition != null) { %>
      <H3> Search results for  <I> <%= searchCondition %> </I> </H3>
      <B> <%= runQuery(searchCondition) %> </B> <HR><BR>
<% }  %>
<B>Enter a search condition:</B>
<FORM METHOD="get"> 
<INPUT TYPE="text" NAME="cond" SIZE=30>
<INPUT TYPE="submit" VALUE="Ask Oracle");
</FORM>
</BODY>
</HTML>
<%-- Declare and define the runQuery() method. --%>
<%! private String runQuery(String cond) throws SQLException {
     Connection conn = null; 
     Statement stmt = null; 
     ResultSet rset = null; 
     try {
        DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
        conn = DriverManager.getConnection("jdbc:oracle:oci:@",
                                           "scott", "tiger");
        stmt = conn.createStatement();
        // dynamic query
        rset = stmt.executeQuery ("SELECT ename, sal FROM scott.emp "+ 
                           (cond.equals("") ? "" : "WHERE " + cond ));
       return (formatResult(rset));
     } catch (SQLException e) { 
         return ("<P> SQL error: <PRE> " + e + " </PRE> </P>\n");
     } finally {
         if (rset!= null) rset.close(); 
         if (stmt!= null) stmt.close();
         if (conn!= null) conn.close();
     }
  }
  private String formatResult(ResultSet rset) throws SQLException {
    StringBuffer sb = new StringBuffer();
    if (!rset.next())
      sb.append("<P> No matching rows.<P>\n");
    else {  sb.append("<UL>"); 
            do {  sb.append("<LI>" + rset.getString(1) + 
                            " earns $ " + rset.getInt(2) + ".</LI>\n");
            } while (rset.next());
           sb.append("</UL>"); 
    }
    return sb.toString();
  }
%>
hope this helps.
Reply With Quote
  #4  
Old 28-02-2009
Member
 
Join Date: Dec 2008
Posts: 69
Re: How JSP call JDBC database connection

Try using this it will helpful to create you the database connection

Code:
conn = DriverManager.getConnection (""jdbc:mysql://localhost/experiment1?user=root&password=root");
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How JSP call JDBC database connection"
Thread Thread Starter Forum Replies Last Post
How to make Database connection in ASP.NET Neil'o Software Development 4 16-11-2011 11:06 AM
How to store byte array to database with JDBC? RogerFielden Software Development 3 23-09-2009 10:52 AM
How to make the Database connection in PHP AK_Chopra Software Development 3 05-09-2009 09:35 AM
How to call SQL database with JSP? Dumshell Software Development 3 03-08-2009 11:18 PM
How to create Database connection? Majestic Software Development 2 18-11-2008 03:31 PM


All times are GMT +5.5. The time now is 05:28 PM.