[opencms-dev] how connect to database from jsp ?

Mark bef5471 at iperbole.bologna.it
Fri Nov 12 22:53:48 CET 2004


Hi Guys!
Which is the better way to get a connection to the opencms database in 
order to
submit a query from a JSP page?
I tested the code below with success, but I suppose it is not the best 
way when
multiple connection take place at the same time: a connection taken from 
a pool
of pooled connections whould be more performant. Can someone rewrite the 
code
below in a more correct way?
Thank's in advance,
I'm new here and I do apologize for my incert English (I'm Italian).
Marco.

<%@ page import = 
"java.sql.Connection,java.sql.DriverManager,java.sql.SQLException,
                   java.sql.Statement, java.sql.ResultSet, 
java.util.*,java.io.*"
%>
<%
    Connection myconn;
    String DbDriver = "org.gjt.mm.mysql.Driver";
    String DbConStr = "jdbc:mysql://localhost:3306/opencms";
    String DbUser ="user";
    String DbPwd ="pwd";
        try {
            Class.forName(DbDriver);
            myconn = DriverManager.getConnection(DbConStr, DbUser, DbPwd);
            Statement mystat = myconn.createStatement();
            ResultSet rs = mystat.executeQuery("SELECT name,years FROM 
people");
            while (rs.next()) {
                 String s = rs.getString("name");
                 int n = rs.getInt("years");
                 out.println(s+"<b>"+n+"</b>");
         }
         myconn.close();
    } catch (SQLException e) {
           out.println("<p>Exception</p>");
     } catch (ClassNotFoundException e) {
           out.println("<p>Exception</p>");
     }
%>



More information about the opencms-dev mailing list