[求助]连接池封装成公共类的问题 struts本身的连接池使用方法就很适合你的这个要求啦。找它的例子看一下你就明白了。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 struts一般是在每个action类里面进行连接的,我就是想把它独立出来package employees;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import org.apache.struts.action.Action;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionForward;import org.apache.struts.action.ActionMapping;import org.apache.struts.action.ActionErrors;import org.apache.struts.action.ActionError;import javax.sql.DataSource;import java.sql.Connection;import java.sql.Statement;import java.sql.ResultSet;import java.sql.SQLException;public class LoginAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { String user = null; // Default target to success String target = new String("success"); // Use the LoginForm to get the request parameters String username = ((LoginForm)form).getUsername(); String password = ((LoginForm)form).getPassword(); DataSource dataSource = (DataSource) servlet.getServletContext().getAttribute("evocdb"); user = getUser(username, password); // Set the target to failure if ( user == null ) { target = new String("login"); ActionErrors errors = new ActionErrors(); errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("errors.login.unknown", username)); // Report any errors we have discovered back to the original form if (!errors.isEmpty()) { saveErrors(request, errors); } } else { HttpSession session = request.getSession(); session.setAttribute("USER", user); } // Forward to the appropriate View return (mapping.findForward(target)); } protected String getUser(String username, String password) { String user = null; Connection conn = null; Statement stmt = null; ResultSet rs = null; DataSource dataSource = (DataSource) servlet.getServletContext().getAttribute("evocdb"); try { conn = dataSource.getConnection(); stmt = conn.createStatement(); String sql = "select * from employees where username=\'" + username + "' " + "and password='" + password + "'"; rs = stmt.executeQuery(sql);// DbLink dl = new DbLink();// rs = dl.DbQuery(sql); if ( rs.next() ) { user = rs.getString("username"); // Iterate over the results System.err.println("Username : " + rs.getString("username") + " Password : " + rs.getString("password")); } else { System.err.println("---->User not found<----"); } } catch (SQLException e) { System.err.println(e.getMessage()); } finally { if (rs != null) { try { rs.close(); } catch (SQLException sqle) { System.err.println(sqle.getMessage()); } rs = null; } 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 user; }} struts数据库连接池,不建议使用,不知道原因。apahe有个DBCP就是干这个的 那用什么方式连接数据库效率最好呢我现在是直接用JDBC连接的,可能每次连接花费的时间比较长点了~ jsp中如何实现文件上传? 如果Date什么都不填应该怎样处理呢? java调用oracle备份脚本的问题 (急!!死!!人!!) HSSFWorkbook生成excel出现的问题 hibernate多对多导致json无法转换问题 高分求稳定,好用的,有人用过的jsp,mysql留言本,急用,先谢了! 如何定义表格中表元的click事件? 关于缓存的问题 不知有哪位高手知道这个问题? 如何实现时间循环? JSP和Bean的问题! 救我,tomcat问题,高手救我
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionError;import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;public class LoginAction extends Action { public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException { String user = null; // Default target to success
String target = new String("success"); // Use the LoginForm to get the request parameters
String username = ((LoginForm)form).getUsername();
String password = ((LoginForm)form).getPassword(); DataSource dataSource = (DataSource)
servlet.getServletContext().getAttribute("evocdb"); user = getUser(username, password); // Set the target to failure
if ( user == null ) { target = new String("login");
ActionErrors errors = new ActionErrors(); errors.add(ActionErrors.GLOBAL_ERROR,
new ActionError("errors.login.unknown",
username)); // Report any errors we have discovered back to the original form
if (!errors.isEmpty()) { saveErrors(request, errors);
}
}
else { HttpSession session = request.getSession();
session.setAttribute("USER", user);
}
// Forward to the appropriate View
return (mapping.findForward(target));
} protected String getUser(String username, String password) { String user = null;
Connection conn = null;
Statement stmt = null;
ResultSet rs = null; DataSource dataSource = (DataSource)
servlet.getServletContext().getAttribute("evocdb"); try { conn = dataSource.getConnection();
stmt = conn.createStatement();
String sql = "select * from employees where username=\'"
+ username + "' " + "and password='" + password + "'";
rs = stmt.executeQuery(sql);
// DbLink dl = new DbLink();
// rs = dl.DbQuery(sql); if ( rs.next() ) { user = rs.getString("username");
// Iterate over the results
System.err.println("Username : "
+ rs.getString("username")
+ " Password : " + rs.getString("password"));
}
else { System.err.println("---->User not found<----");
}
}
catch (SQLException e) { System.err.println(e.getMessage());
}
finally { if (rs != null) { try { rs.close();
}
catch (SQLException sqle) { System.err.println(sqle.getMessage());
}
rs = null;
}
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 user;
}
}
apahe有个DBCP就是干这个的
我现在是直接用JDBC连接的,可能每次连接花费的时间比较长点了~