在你的Action中执行相关的数据库操作.如下:
public ActionForward
execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception
{
javax.sql.DataSource dataSource;
java.sql.Connection myConnection;
try {
dataSource = getDataSource(request);
myConnection = dataSource.getConnection();
// do what you wish with myConnection
} catch (SQLException sqle) {
getServlet().log("Connection.process", sqle);
} finally {
//enclose this in a finally block to make
//sure the connection is closed
try {
myConnection.close();
} catch (SQLException e) {
getServlet().log("Connection.close", e);
}
}
}
同时你的配置文件中要进行如下配置:
<data-sources>
<!-- configuration for commons BasicDataSource -->
<data-source type="org.apache.commons.dbcp.BasicDataSource">
<set-property
property="driverClassName"
value="org.postgresql.Driver" />
<set-property
property="url"
value="jdbc:postgresql://localhost/mydatabase" />
<set-property
property="username"
value="me" />
<set-property
property="password"
value="test" />
<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 et" />
</data-source>
</data-sources>
自己照着struts中的介绍写吧!
public ActionForward
execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception
{
javax.sql.DataSource dataSource;
java.sql.Connection myConnection;
try {
dataSource = getDataSource(request);
myConnection = dataSource.getConnection();
// do what you wish with myConnection
} catch (SQLException sqle) {
getServlet().log("Connection.process", sqle);
} finally {
//enclose this in a finally block to make
//sure the connection is closed
try {
myConnection.close();
} catch (SQLException e) {
getServlet().log("Connection.close", e);
}
}
}
同时你的配置文件中要进行如下配置:
<data-sources>
<!-- configuration for commons BasicDataSource -->
<data-source type="org.apache.commons.dbcp.BasicDataSource">
<set-property
property="driverClassName"
value="org.postgresql.Driver" />
<set-property
property="url"
value="jdbc:postgresql://localhost/mydatabase" />
<set-property
property="username"
value="me" />
<set-property
property="password"
value="test" />
<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 et" />
</data-source>
</data-sources>
自己照着struts中的介绍写吧!
struts与jdbc的联系与区别到底在哪里?(在线等待......) 谢谢!
/**
*表示用户信息,它是一个值对象,被From使用
*这个对象必须提供对应的getter和setter方法
*/
public class User
{
public String userName;
public String password;
public int age;
public void setUserName(String name)
{
this.userName=name;
}
public void setPassword(String password)
{
this.password=password;
}
public String getUserName()
{
return this.userName;
}
public String getPassword()
{
return password;
}
public int getAge()
{
return this.age;
}
public void setAge(int age)
{
this.age=age;
}
}
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.*;//Form,包含一个user属性。public class UserForm extends ActionForm
{
private User user=new User();
public void setUserName(String userName)
{
user.setUserName(userName);
}
public String getUserName()
{
return user.getUserName();
}
public void setUser(User user)
{
this.user=user;
}
public User getUser()
{
return this.user;
}
//重新设置form
public void reset(ActionMapping mapping, HttpServletRequest request)
{
this.user = new User();
}
//form有效性判断,这里做简化处理。
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request)
{
ActionErrors errors = new ActionErrors();
if ((user.getUserName() == null) || (user.getPassword().length() < 3))
{
errors.add("password", new ActionError("error.user.password"));
}
return errors;
}
}
import org.apache.struts.action.*;
import javax.servlet.http.*;
//Action类。
public class UserAction extends Action
{
public ActionForward perform(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response)
{
UserForm f=(UserForm)form;
try
{
UserBean bean=new UserBean();//UserBean连接数据库,执行具体的业务逻辑。
bean.addUser(f.getUser());
}
catch(Exception e)
{
e.printStackTrace();
}
request.setAttribute("User",f.getUser());
return (mapping.findForward("userCreated"));
}
}
*****************************
package com.jspdev;
import com.jspdev.util.*;
import java.sql.*;
import javax.sql.*;
import java.io.*;
/**
*实现具体的业务逻辑,它被Action类调用
*并且和HttpRequset等无关,这样增加了重用性。
*/
public class UserBean
{
private Connection conn ;
public UserBean()throws Exception
{
this.conn=DatabaseConn.getConnection();
}
/**
*在数据库中添加一个用户
*/
public void addUser(User user)throws Exception
{
Statement stmt=conn.createStatement();
stmt.executeUpdate("insert into StrutsUser values('"+user.getUserName()+"','"+user.getPassword()+"',"+user.getAge()+")");
}
}
*****************这个是用于输入用户信息的jsp****************************
<%@page contentType="text/html;charset=gb2312"%>
<%@ taglib uri="/struts-logic" prefix="logic" %>
<%@ taglib uri="/struts-bean" prefix="bean" %>
<%@ taglib uri="/struts-html" prefix="html" %>
<html:html locale="true">
<head>
<html:base/>
<title>
<bean:message key="index.title"/>
</title>
</head>
<body>
<h2>创建一个用户</h2>
<html:errors/>
<html:form action="createUser.do" method="GET">
userName:<html:text property="user.userName" /> <br/>
password:<html:password property="user.password" /> <br/>
age: <html:text property="user.age" /> <br/>
<html:submit property="submit"/>
</html:form>
</body>
</html:html>
**********************************************
<%@page contentType="text/html;charset=gb2312" import="com.jspdev.User"%>
<%@ taglib uri="/struts-logic" prefix="logic" %>
<%@ taglib uri="/struts-bean" prefix="bean" %>
<%@ taglib uri="/struts-html" prefix="html" %><html:html locale="true">
<head>
<html:base/>
<title>
<bean:message key="index.title"/>
</title>
</head>
<body>
<%
User user=(User)request.getAttribute("User");
%>
已经创建了用户:
Name:<%=user.getUserName()%><br>
password:xxxxx<br>
age:<%=user.getAge()%><br>
</body>
</html:html>
***********************
下面是struts-config.xml文件
*****************************
<?xml version="1.0" encoding="ISO-8859-1" ?><!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<form-beans>
<form-bean name="userForm" type="com.jspdev.UserForm"/>
</form-beans>
<global-forwards>
<forward name="userCreated" path="/viewUser.jsp"/>
</global-forwards>
<action-mappings>
<action path="/createUser"
type="com.jspdev.UserAction"
name="userForm"
scope="request"
validate="true"
input="/createUser.jsp">
</action>
create table StrutsUser(userName varchr(25),password varchar(25),get int)然后启动web服务器就可以访问createUser.jsp