首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

继续STRUTS下,数据源配置有关问题,昨天那个帖子被人恶意加了一大段广告,气愤啊

2012-01-06 
继续STRUTS下,数据源配置问题,昨天那个帖子被人恶意加了一大段广告,气愤啊!!!--这里是关于连接池的配置,

继续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 ")是空的,感觉问题在这里,但是不知道怎么解决,有高手帮忙看一下啊!!

[解决办法]
context.getAttribute( "sqlserver ")返回是空??
用异常捕捉一下看具体是什么?
要不改下
ServletContext context = servlet.getServletContext();//看着this有点怪 去掉试下
DataSource dataSource = (DataSource)context.getAttribute( "sqlserver ");

还有struts1.2 datasource 中好象user好象是换成username了 并且也不提倡使用datasource
[解决办法]
我就知道这2个,你试试:
在Action中:
this.getDataSource(request, "sqlserver ");
其他地方:
request.getSession().getServletContext().getAttribute( "sqlserver ");
[解决办法]
<set-property property= "user " value= "sa "/>
中的user改成username
[解决办法]
楼上正解,myeclipse生成的代码有错误

热点排行