Results 1 to 4 of 4

Thread: How JSP call JDBC database connection

  1. #1
    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

  2. #2
    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());

  3. #3
    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.

  4. #4
    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");

Similar Threads

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

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,519,476.35780 seconds with 16 queries