struts中连接数据库

本文介绍如何在Struts框架中配置数据库连接,并演示了一个具体的登录验证Action示例,包括从数据库查询用户信息的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在struts中连接数据库,并且在action操作数据库,需要在struts.xml中进行配置,具体配置如下:
	<data-sources>
<data-source type="org.apache.commons.dbcp.BasicDataSource">
<set-property property="driverClassName"
value="com.mysql.jdbc.Driver" />
<set-property property="url"
value="jdbc:mysql://localhost:3306/demo" />
<set-property property="username" value="root" />
<set-property property="password" value="" />
<set-property property="maxActive" value="10" />
<set-property property="maxWait" value="5000" />
<set-property property="defaultAutoCommit" value="false" />
<set-property property="defaultReadOnly" value="false" />
<set-property property="validationQuery"
value="SELECT COUNT(*) FROM user" />
</data-source>
</data-sources>

之后就可以在action中访问数据库了,具体方式如下:
package com.demo.struts.actions;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.actions.DispatchAction;

import com.demo.struts.forms.LoginForm;
import com.demo.struts.util.Constants;

public class LoginAction extends DispatchAction {

Logger log = Logger.getLogger(this.getClass());

@SuppressWarnings("deprecation")
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {

ActionErrors errors = new ActionErrors();
ActionForward forward = new ActionForward();
LoginForm loginForm = (LoginForm) form;

try {
// get parameters
String username = loginForm.getUsername();

// invalidate the original session if exists
HttpSession session = request.getSession(false);
if (session != null) {
session.invalidate();
}

// create a new session for the user
session = request.getSession(true);

// login
boolean isValid = valid(request, loginForm);
if (isValid) {
session.setAttribute(Constants.USERNAME_KEY, username);

log.info("User " + username + " login.");
} else {
errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionMessage(
"login.message.failed"));
}

} catch (Exception e) {
errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionMessage(
"login.message.failed"));

}

// If a message is required, save the specified key(s)
// into the request for use by the <struts:errors> tag.

if (!errors.isEmpty()) {
saveErrors(request, errors);
request.setAttribute("loginFormBean", loginForm);
forward = mapping.findForward(Constants.FAILURE_KEY);
} else {
forward = mapping.findForward(Constants.SUCCESS_KEY);
}

// Finish with
return (forward);
}

private boolean valid(HttpServletRequest request, LoginForm loginForm) {
DataSource ds = null;
Connection cn = null;
boolean b = false;
try {
ds = getDataSource(request);
cn = ds.getConnection();
Statement st = cn.createStatement();
ResultSet rs = st
.executeQuery("select * from user where username='"
+ loginForm.getUsername() + "' and password='"
+ loginForm.getPassword() + "'");
if (rs.next()) {
b = true;
}
rs.close();
st.close();
cn.close();
} catch (Exception e) {
e.printStackTrace();
}
return b;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值