Struts数据源配置过程的详细介绍
JDBC数据源(Data Source)的使用,简化了数据库的连接过程.
Struts中使用JDBC 2.0标准扩展包中的javax.sql.DataSource接口来实现数据源的。
扩展包是jdbc2_0-stdext.jar。,在jdk1.4后版本包含此扩展包。
例如下面的例子。数据库名“classuser”,表:“classuser”。
在Struts配置文件“struts-config.xml中”配置Struts数据源,这些数据源由ActionServlet负责管理。
以下代码是Struts-config.xml的配置。
<struts-config>
<data-sources>
<data-source key="oracleDB1" type="org.apache.commons.dbcp.BasicDataSource">
<set-property property="driverClassName"
value="com.microsoft.jdbc.sqlserver.SQLServerDriver" />
<set-property property="url"
value="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=classuser;" />
<set-property property="maxActive"
value="5"/>
<set-property property="username"
value="sa"/>
<set-property property="password"
value=""/>
<set-property property="autoCommit"
value="true"/>
</data-source>
</data-sources>
<data-source key="oracleDB1" type="org.apache.commons.dbcp.BasicDataSource">
<set-property property="driverClassName"
value="com.microsoft.jdbc.sqlserver.SQLServerDriver" />
<set-property property="url"
value="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=classuser;" />
<set-property property="maxActive"
value="5"/>
<set-property property="username"
value="sa"/>
<set-property property="password"
value=""/>
<set-property property="autoCommit"
value="true"/>
</data-source>
</data-sources>
定义模型类DBUser,在该类中封装了与数据库操作相关的业务逻辑。
package classmate;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBUser {
DataSource dataSource;
public DBUser(DataSource dataSource) {
this.dataSource = dataSource;
}
public boolean checkUser(String name,String psw) throws Exception{
Connection connect = null;
String strSql;
ResultSet rs;
boolean result=false;
strSql = "select * from classuser where username='"
+ name + "' and password='" + psw + "'";
try {
connect = dataSource.getConnection();
Statement stmt = connect.createStatement();
rs = stmt.executeQuery(strSql);
if ( rs.next()) {
result=true;
}
}
catch(SQLException ex) {
ex.printStackTrace();
}
finally{
if(connect!=null)
connect.close();
}
return result;
}
}
public DBUser(DataSource dataSource) {
this.dataSource = dataSource;
}
public boolean checkUser(String name,String psw) throws Exception{
Connection connect = null;
String strSql;
ResultSet rs;
boolean result=false;
strSql = "select * from classuser where username='"
+ name + "' and password='" + psw + "'";
try {
connect = dataSource.getConnection();
Statement stmt = connect.createStatement();
rs = stmt.executeQuery(strSql);
if ( rs.next()) {
result=true;
}
}
catch(SQLException ex) {
ex.printStackTrace();
}
finally{
if(connect!=null)
connect.close();
}
return result;
}
}
改造Action Bean类LoginAction,在其中使用前面配置的数据源oracleDB1。
ServletContext context = servlet.getServletContext();
DataSource dataSource = (DataSource)context.getAttribute("oracleDB1");
DBUser dbuser = new DBUser(dataSource);
HttpSession session = request.getSession();
if (!dbuser.checkUser(name,psw)) {
ActionMessages errors = new ActionMessages();
errors.add(ActionMessages.GLOBAL_MESSAGE,
new ActionMessage("label.deny"));
if (!errors.isEmpty()) {
saveErrors(request, errors);
}
return mapping.findForward("failed"); //登陆失败
}
else{
return (mapping.findForward("successed"));//登陆成功
}
}
}
DataSource dataSource = (DataSource)context.getAttribute("oracleDB1");
DBUser dbuser = new DBUser(dataSource);
HttpSession session = request.getSession();
if (!dbuser.checkUser(name,psw)) {
ActionMessages errors = new ActionMessages();
errors.add(ActionMessages.GLOBAL_MESSAGE,
new ActionMessage("label.deny"));
if (!errors.isEmpty()) {
saveErrors(request, errors);
}
return mapping.findForward("failed"); //登陆失败
}
else{
return (mapping.findForward("successed"));//登陆成功
}
}
}
此外,在Struts架构中,根据需要也允许在配置文件中声明多个数据源,此时需要使用“key”属性为每一个数据源分配一个唯一的key值,标示特定数据源。
本文地址:http://www.45fan.com/dnjc/69299.html