站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Java 专题在servlet中用连接池DDConnectionBroker

在servlet中用连接池DDConnectionBroker

摘要: 在servlet中用连接池DDConnectionBroker

</td> </tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> 下面用到的连接代理DDConnectionBroker,请从http://opensource.devdaily.com/下载其jar文件DDConnectionBroker.jar。

<table width="676" border="0"> <tr> <td width="370"> 先写一个Singleton(单态)类,以便在服务器中共享连接池。

package examples;

import com.devdaily.opensource.database.DDConnectionBroker;

import java.io.*;

import java.sql.*;

/**
/* This is our class that will be shared across all of the
* servlets for the 'Lavender' database. It is a singleton,
* and also works as an adapter to the connection broker
* class that we are using.
*/ </td> <td width="296"> </td> </tr> </table>
public class LavenderDBSingleton
{

private DDConnectionBroker m_broker;
private static LavenderDBSingleton m_singleton = null;

private LavenderDBSingleton()
{
/*
* We will put all of our database-specific information
* here. Please note that we could have read this
* information from a properties file.
*/

String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
String url = "jdbc:odbc:Lavender";
String uname = "";
String passwd = "";

int minConnections = 1;
int maxConnections = 10;
long timeout = 100;
long leaseTime = 60000;
String logFile = "c:/tmp/ConnectionPool.log";

try
{
m_broker = new DDConnectionBroker(driver,
url, uname, passwd,
minConnections,
maxConnections,
timeout,
leaseTime,
logFile);
}
catch (SQLException se)
{
System.err.println( se.getMessage() );
}
}
/**
* getInstance() returns the class, instantiating it
* if there is not yet an instance in the VM.
*/
public static LavenderDBSingleton getInstance()
{
if (m_singleton == null)
{
m_singleton = new LavenderDBSingleton();
}

return (m_singleton);
}

/*
* calls getConnection() on the broker class
*/
public synchronized Connection getConnection() throws Exception
{
if (m_broker == null)
{
throw new Exception("Can't get Connection broker!");
}
return (m_broker.getConnection());
}

/*
* frees the connection from the broker class
*/
public synchronized void freeConnection(Connection con)
throws Exception
{
if (m_broker == null )
{
throw new Exception("Can't get Connection broker!");
}
m_broker.freeConnection(con);
}
}

下面是测试代码:

package examples;

import java.io.*;
import java.sql.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class BestQueryServlet extends HttpServlet
{
//only set in the init() method, so concurrency
//issues should be fine.
private LavenderDBSingleton m_dbsingleton = null;

public void init()
{
/*
* This will instantiate it within the Servlet's
* virtual machine if it hasn't already. If it
* has, we have the instance of it.
*/
m_dbsingleton = LavenderDBSingleton.getInstance();
}
/**
* simply forwards all to doPost()
*/
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
doPost(request,response);
}

/**
* The main form!
*/
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
PrintWriter out = response.getWriter();

out.println("Internal Inventory Check");
out.println("<body BGCOLOR='white'>");
out.println("

Lavender Fields Farm Internal Inventory

");

//show the date.
SimpleDateFormat sdf =new SimpleDateFormat ("EEE, MMM d, yyyy h:mm a");
java.util.Date newdate = new java.util.Date(Calendar.getInstance().getTime().getTime());
String datestring = sdf.format(newdate);

out.println("

Inventory as of: " + datestring + "

");

out.println("<table BORDER=1>");
out.println("<tr><td BGCOLOR='yellow'>" +
"
Name
</td>" +
"<td BGCOLOR='yellow'>" +
"
Description
</td>" +
"<td BGCOLOR='yellow'>" +
"
Inventory Amount
</td></tr>");

//Load the inventory from the database.

try
{

Connection con = m_dbsingleton.getConnection();
if (con == null)
{
out.println("There are currently database problems. " +
"Please see your administrator for details.
");
return;
}


Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from Inventory");

while (rs.next())
{
String amtString = "";
int amt = rs.getInt("Amount");
if (amt < 50)
amtString ="<td>
" +
amt + "
</td>";
else
amtString ="<td>
" +
amt + "
</td>";

out.println("<tr><td>
" + rs.getString("Name") +
"
</td><td>
" +
rs.getString("Description") +
"
</td>" + amtString + "</tr>");
}
rs.close();
out.println("</table>
Items in RED" +
" denote a possible low inventory. Click Here to " +
" contact " +
"MANAGEMENT to order more supplies.");

//Free the connection!
m_dbsingleton.freeConnection( con );

}
catch (Exception e)
{
out.println("There were errors connecting to the database." +
" Please see your systems administrator for details.");
e.printStackTrace();
}

}

}

在我们的应用中需要连接时,只需简单地象下面这样:

LavenderDBSingleton singleton=LavenderDBSingleton.getInstance();
Connection con=singleton.getConnection();
try{
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from Inventory");
//do something
singleton.freeConnection(con);
}catch(Exception e){
//......
}
</td> </tr> <tr>


↑返回目录
前一篇: 版权过滤器
后一篇: 从Servlet读取文件