本帖最后由 pzfpang449 于 2013-1-5 17:51 编辑
今天自学的是struts中配置DBCP连接池,并用他做了一个登陆和注册的程序 首先要下载JAR包支持,commons-dbcp-1.2.1,jar和commons-pool-1.2.jar还有mysql的驱动软件包mysql-connector-java-5.0.8-bin.jar将他们导入到lib文件夹中 1在昨天的struts-config.xml文件中添加<data-source> <data-sources>
<data-source type="org.apache.commons.dbcp.BasicDataSource">
<set-property property="driverClassName"
value="org.gjt.mm.mysql.Driver" />
<set-property property="url"
value="jdbc:mysql://localhost:3306/demo" />
<set-property property="username" value="root" />
<set-property property="password" value="root" />
<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>
2在昨天开发的基础上的LoginAction.java文件下添加验证方法 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=............"; if(rs.next()) { b=true; } }catch(Exception e) { e.printStackTrace(); } return b;
} 4注册的页面的修改RegisterAction.java文件添加如下代码进行验证用户名是否存在 private boolean isExist(HttpServletRequest requst,RegisterForm registerForm) { 类似上面登陆,请看下面的源代码 } 5注册代码 private void insert(HttpServletRequest request,RegisterForm registerForm) { 类似
} 源码地址:http://download.csdn.net/detail/zaifeishandong/4550294 (Mysql数据库别忘了创建)
|