读书人

关于STRUTS中配置数据库连接池的有关问

发布时间: 2012-01-14 20:02:35 作者: rapoo

关于STRUTS中配置数据库连接池的问题
<!-- 这里是关于连接池的配置,使用SQLSERVER2000那个TYPE包是我自己下载打进去的,我用的环境是JDK1.6+TOMCAT6.X+ECLIPSE3.2.2 -->


<?xml version= "1.0 " encoding= "UTF-8 "?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN " "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd ">

<struts-config>

<!-- Data Sources Configuration -->
<data-sources>
<data-source key= "sqlserver " type= "org.apache.commons.dbcp.BasicDataSource ">
<set-property property= "driverClassName " value= "com.microsoft.jdbc.sqlserver.SQLServerDriver "/>
<set-property property= "url " value= "jdbc:microsoft:sqlserver://localhost:1433;databasename=user "/>
<set-property property= "maxActive " value= "5 "/>
<set-property property= "user " value= "sa "/>
<set-property property= "password " value= "sa "/>
<set-property property= "autoCommit " value= "true "/>
</data-source>
</data-sources>

<!-- ActionForm Configuration-->
<form-beans>
<form-bean name= "formBean1 " type= "user.UserForm "> </form-bean>
</form-beans>

<!-- GlobalForward Configuration -->
<global-forwards>
<forward name= "successed " path= "/right.jsp "/>
<forward name= "failed " path= "/error.jsp "/>
</global-forwards>

<!-- Action Configuration -->
<action-mappings>
<action path= "/logincheck " type= "user.LoginCheck " name= "formBean1 " scope= "request " input= "/right.jsp "/>
</action-mappings>
</struts-config>


DBUSER类,具体的业务模型
package user;
import javax.sql.*;
import java.sql.*;
public class DBUser
{
DataSource dataSource;
public DBUser(DataSource dataSource)
{
this.dataSource = dataSource;
}
public boolean checkUser(String username,String password) throws Exception
{
Connection connection = null;
String StrSql;
ResultSet rs;
boolean result = false;
StrSql = "select * from users where username= ' " + username + " 'and password= ' " + password + " ' ";
try
{
connection = dataSource.getConnection();
Statement stmt = connection.createStatement();
rs = stmt.executeQuery(StrSql);
if(rs.next())
{
result = true;
}
}
catch(SQLException ex)
{
ex.printStackTrace();
}
finally
{
if(connection != null)
{
connection.close();
}
}
return result;
}
}


这个是ACTION BEAN
package user;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.*;
import javax.sql.DataSource;
import javax.servlet.http.HttpSession;
public final class LoginCheck extends Action
{
public ActionForward execute
(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response
) throws Exception
{
UserForm userform = (UserForm) form;
String username = userform.getUsername();
String password = userform.getPassword();
ServletContext context = this.servlet.getServletContext();
DataSource dataSource = (DataSource)context.getAttribute( "sqlserver ");
DBUser dbuser = new DBUser(dataSource);
//HttpSession session = request.getSession();



if(dbuser.checkUser(username, password))
{
return mapping.findForward( "successed ");
}
else
{
return mapping.findForward( "failed ");
}
}
}


现在的错误提示就是告诉我ACTIONBEAN 类下面调用数据库连接池的部分有错

DBUSER类中的connection = dataSource.getConnection();
LOGINACTION类中的if(dbuser.checkUser(username, password)){}


我在网上查了半天了,别人好像也都是这么写的,而且我自己测试的,发现CONTEXT不是空的,但是context.getAttribute( "sqlserver ")是空的,感觉问题在这里,但是不知道怎么解决,有高手帮忙看一下啊!!

[解决办法]
ServletContext context = getServlet().getServletContext();
ModuleConfig moduleConfig = RequestUtils.getModuleConfig(request, context);
DataSource dataSource = (DataSource)context.getAttribute(key + moduleConfig.getPrefix());
这样看看
[解决办法]
<set-property property= "user " value= "sa "/>
user改成username

读书人网 >Java Web开发

热点推荐