在struts-config.xml中加入:
<data-sources>
<data-source type="org.apache.struts.util.GenericDataSource">
<set-property value="false" property="autoCommit" />
<set-property value="Example Data Source Configuration" property="description" />
<set-property value="oracle.jdbc.driver.OracleDriver" property="driverClass" />
<set-property value="100" property="maxCount" />
<set-property value="10" property="minCount" />
<set-property value="rillet" property="password" />
<set-property value="jdbc:oracle:thin:@192.168.0.11:1521:orcl" property="url" />
<set-property value="rillet" property="user" />
</data-source>
</data-sources>然后在action中应用:
下面是个例子:)
import org.apache.struts.action.*;
import javax.servlet.http.*;import java.sql.*;
import javax.sql.*;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.ArrayList;
import java.sql.SQLException;
import java.io.UnsupportedEncodingException;import java.text.SimpleDateFormat;
import java.text.DateFormat;public class ViewDbAction extends Action { DataSource datasource = null;
//convert rs to arraylist
public ArrayList Rstolist(ResultSet rs) throws SQLException,
UnsupportedEncodingException { ArrayList rows = new ArrayList(); try {
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount(); while (rs.next()) {
HashMap row = new HashMap();
for (int i = 1; i <= columnCount; i++) {
String name = rsmd.getColumnName(i);
row.put(name, rs.getString(i));
System.out.println(row.get(name));
}
rows.add(row);
}
}
catch (SQLException e) {
System.err.println(e.getMessage());
}
return rows;
} //query database to rs
public ArrayList QuerySQL(String strSql) throws UnsupportedEncodingException,
SQLException { ArrayList m_rows = new ArrayList();
m_rows = null;
Connection conn = null;
Statement stmt = null;
ResultSet m_rs = null;
try {
conn = datasource.getConnection();
stmt = conn.createStatement();
System.out.println("1:" + strSql); m_rs = stmt.executeQuery(strSql);
m_rows = Rstolist(m_rs); }
catch (SQLException e) {
System.err.println(e.getMessage());
}
finally {
if (stmt != null) {
try {
stmt.close();
}
catch (SQLException sqle) {
System.err.println(sqle.getMessage());
}
stmt = null;
}
if (conn != null) {
try {
conn.close();
}
catch (SQLException sqle) {
System.err.println(sqle.getMessage());
}
conn = null;
}
}
return m_rows;
}
public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
throws Exception {
/**@todo: complete the business logic here, this is just a skeleton.*/
datasource = getDataSource(httpServletRequest);
System.out.println("datasource has been gotten");
///////////////////////////////////////////////////////////////
java.util.Date date = new java.util.Date();
String now_date = new SimpleDateFormat("yyyyMMdd").format(date); String str = "select * from supper where book_date='"+now_date+"'";
System.out.println("0:" + str);
ArrayList v_rows = QuerySQL(str); httpServletRequest.setAttribute("myrows", v_rows);
//////////////////////////////////////////////////////////// return actionMapping.findForward("success");
}
}
<data-sources>
<data-source type="org.apache.struts.util.GenericDataSource">
<set-property value="false" property="autoCommit" />
<set-property value="Example Data Source Configuration" property="description" />
<set-property value="oracle.jdbc.driver.OracleDriver" property="driverClass" />
<set-property value="100" property="maxCount" />
<set-property value="10" property="minCount" />
<set-property value="rillet" property="password" />
<set-property value="jdbc:oracle:thin:@192.168.0.11:1521:orcl" property="url" />
<set-property value="rillet" property="user" />
</data-source>
</data-sources>然后在action中应用:
下面是个例子:)
import org.apache.struts.action.*;
import javax.servlet.http.*;import java.sql.*;
import javax.sql.*;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.ArrayList;
import java.sql.SQLException;
import java.io.UnsupportedEncodingException;import java.text.SimpleDateFormat;
import java.text.DateFormat;public class ViewDbAction extends Action { DataSource datasource = null;
//convert rs to arraylist
public ArrayList Rstolist(ResultSet rs) throws SQLException,
UnsupportedEncodingException { ArrayList rows = new ArrayList(); try {
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount(); while (rs.next()) {
HashMap row = new HashMap();
for (int i = 1; i <= columnCount; i++) {
String name = rsmd.getColumnName(i);
row.put(name, rs.getString(i));
System.out.println(row.get(name));
}
rows.add(row);
}
}
catch (SQLException e) {
System.err.println(e.getMessage());
}
return rows;
} //query database to rs
public ArrayList QuerySQL(String strSql) throws UnsupportedEncodingException,
SQLException { ArrayList m_rows = new ArrayList();
m_rows = null;
Connection conn = null;
Statement stmt = null;
ResultSet m_rs = null;
try {
conn = datasource.getConnection();
stmt = conn.createStatement();
System.out.println("1:" + strSql); m_rs = stmt.executeQuery(strSql);
m_rows = Rstolist(m_rs); }
catch (SQLException e) {
System.err.println(e.getMessage());
}
finally {
if (stmt != null) {
try {
stmt.close();
}
catch (SQLException sqle) {
System.err.println(sqle.getMessage());
}
stmt = null;
}
if (conn != null) {
try {
conn.close();
}
catch (SQLException sqle) {
System.err.println(sqle.getMessage());
}
conn = null;
}
}
return m_rows;
}
public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
throws Exception {
/**@todo: complete the business logic here, this is just a skeleton.*/
datasource = getDataSource(httpServletRequest);
System.out.println("datasource has been gotten");
///////////////////////////////////////////////////////////////
java.util.Date date = new java.util.Date();
String now_date = new SimpleDateFormat("yyyyMMdd").format(date); String str = "select * from supper where book_date='"+now_date+"'";
System.out.println("0:" + str);
ArrayList v_rows = QuerySQL(str); httpServletRequest.setAttribute("myrows", v_rows);
//////////////////////////////////////////////////////////// return actionMapping.findForward("success");
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货