[opencms-dev] Addressing external database from JSP

Stephan Hartmann beffe at beffe.de
Fri Apr 30 22:07:01 CEST 2004


It should look like this:
<html>
<body>
<%@ page
import = "java.io.*"
import = "java.lang.*"
import = "java.sql.*"
%>

<%!
   static String sDbDrv = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
   static String sDbUrl = "jdbc:microsoft:sqlserver://server/bla";
   String sSql = "SELECT * from Example";

   static {
       Class.forName(sDbDrv);
       }
%>

<%
   Connection cn = DriverManager.getConnection(sDbUrl, "bla", "bla");
   Statement st = cn.createStatement();
   ResultSet rs = st.executeQuery(sSql);

   ResultSetMetaData meta = rs.getMetaData();
   out.println("Got results:");

            while
              (rs.next() == true)
              {
                // Which Column?
                out.println("Working Title: ");
                String a = rs.getString("P_TITEL");
                out.print(a);

                 //Which Column?
                out.print("Projektziel: ");
                String b = rs.getString("P_ZIEL");
                out.print(b);
}
try {
 if (rs!=null) rs.close();
 if (st!=null) st.close();
 if (cn!=null) cn.close();
}
catch (Exception ignore) {}

%>
</body>
</html>

----- Original Message -----
From: "Stephan Hartmann" <beffe at beffe.de>
To: <opencms-dev at opencms.org>
Sent: Friday, April 30, 2004 8:55 PM
Subject: Re: [opencms-dev] Addressing external database from JSP


> Hi Björn,
>
> inside of a definition block (starting with "<%!" ) you can only place
> member variable an method definitions, but not normal code. You have to
> close this block with a "%>" after the method jspInit's closing bracket
and
> open a code block for the rest with "<%".
> After the closing bracket of the while loop you should also place some
clean
> up:
> try {
>  if (rs!=null) rs.close();
>  if (st!=null) st.close();
>  if (cn!=null) cn.close();
> }
> catch (Exception ignore) {}
>
> Bye,
> Stephan
>
> ----- Original Message -----
> From: "Björn Schlueter" <bschlueter at lenord.de>
> To: <opencms-dev at opencms.org>
> Sent: Friday, April 30, 2004 10:40 AM
> Subject: [opencms-dev] Addressing external database from JSP
>
>
> Hello there,
>
> as a newbie I am stuck with this little problem.
> I am trying to access from opencms an external datebase (namely a ms sql
> 2000 server).
> I know I should use a servlet to access that db, but right know i want to
> use a jsp as a shortcut.
>
> This ist what my code looks like:
>
> <html>
> <body>
> <%@ page
> import = "java.io.*"
> import = "java.lang.*"
> import = "java.sql.*"
> %>
>
> <%!
>    String sDbDrv = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
>    String sDbUrl = "jdbc:microsoft:sqlserver://server/bla";
>    String sSql = "SELECT * from Example";
>
>    public void jspInit()
>        {
>        Class.forName(sDbDrv);
>        Connection cn = DriverManager.getConnection(sDbUrl, "bla", "bla");
>        }
>
>    Statement st = cn.createStatement();
>    ResultSet rs = st.executeQuery(sSql);
>
>    ResultSetMetaData meta = rs.getMetaData();
>    out.println("Got results:");
>
>             while
>               (rs.next() == true)
>               {
>                 // Which Column?
>                 out.println("Working Title: ");
>                 String a = rs.getString("P_TITEL");
>                 out.print(a);
>
>                  //Which Column?
>                 out.print("Projektziel: ");
>                 String b = rs.getString("P_ZIEL");
>                 out.print(b);
> }
> %>
> </body>
> </html>
>
>
> I really don't know, why OpenCMS does not want to compile that jsp.
>
> I get the following  messages:
>
> javax.servlet.ServletException: Resource loader error in file
'/testdb.jsp'
>
> Root cause:
> org.apache.jasper.JasperException: Unable to compile class for JSP
>
> An error occurred at line: -1 in the jsp file: null
>
> I am pretty much helpless right know. I couldn't find help at google or
> relevant java forums!
>
> Can somone of you give me a hint?
>
> Thanks
>
> Björn
>
>
> _______________________________________________
> This mail is send to you from the opencms-dev mailing list
> To change your list options, or to unsubscribe from the list, please visit
> http://mail.opencms.org/mailman/listinfo/opencms-dev
>
>
> _______________________________________________
> This mail is send to you from the opencms-dev mailing list
> To change your list options, or to unsubscribe from the list, please visit
> http://mail.opencms.org/mailman/listinfo/opencms-dev
>




More information about the opencms-dev mailing list