Struts 1.3.8 学习笔记(四)
第四个版本我们通过访问数据库来校验用户名和密码!
?
我们使用的数据库MySQL,所以我们要用到MySQL的驱动:mysql-connector-java-5.1.17-bin.jar
?
创建表的脚本
?
-- ------------------------------ Table structure for user-- ----------------------------DROP TABLE IF EXISTS user;CREATE TABLE user ( username varchar(256) NOT NULL, password varchar(256) DEFAULT NULL, PRIMARY KEY (username)) ENGINE=MyISAM DEFAULT CHARSET=UTF-8;-- ------------------------------ Records of user-- ----------------------------INSERT INTO user VALUES ('admin', 'admin');INSERT INTO user VALUES ('user', 'user');
?
?
同时需要JDBC来访问数据库
首先创建一个JDBC工具类,用来建立和关闭连接
JdbcUtil.java
package com.hp.gddc.ad.db;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.Statement;public class JdbcUtil {public static void close(Statement st, Connection con) {try {st.close();} catch (Exception e) {}try {con.close();} catch (Exception e) {}}public static void close(ResultSet rs, Statement st, Connection con) {try {rs.close();} catch (Exception e) {}close(st, con);}public static Connection getConnection() throws Exception {Class.forName(SqlConstant.DB_NAME);return DriverManager.getConnection(SqlConstant.DB_URL, SqlConstant.DB_USER_NAME, SqlConstant.DB_PASSWORD);}}
?
再创建一个常量类,用于保存数据库的连接属性
SqlConstan.java
package com.hp.gddc.ad.db;public class SqlConstant {public static final String DB_NAME = "com.mysql.jdbc.Driver";public static final String DB_URL = "jdbc:mysql://localhost:3306/stu";public static final String DB_USER_NAME = "root";public static final String DB_PASSWORD = "1234";}
?最后就可以通过UserDao来访问数据库了:
UserDao.java
package com.hp.gddc.ad.db;import java.sql.Connection;import java.sql.ResultSet;import java.sql.Statement;public class UserDao {public int queryUser(String username, String password) {Connection con = null;Statement st = null;ResultSet rs = null;int i = 0;try {con = JdbcUtil.getConnection();st = con.createStatement();String sql = "select * from user where username='" + username + "' and password='" + password + "'";System.out.println(sql);rs = st.executeQuery(sql);while (rs.next()) {i++;System.out.print("username=" + rs.getString("username"));System.out.print(",password=" + rs.getString("password"));System.out.println("");}} catch (Exception e) {e.printStackTrace();throw new RuntimeException(e);} finally {JdbcUtil.close(rs, st, con);}return i;}}
?同时更新LoginAction中的相关代码,把硬编码比较改成访问数据库:
LoginAction.java
package com.hp.gddc.ad.action;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;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 com.hp.gddc.ad.db.UserDao;import com.hp.gddc.ad.form.LoginForm;public class LoginAction extends Action {/** * 处理客户端请求 */@Overridepublic ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception {// 得到客户的的提交数据LoginForm lf = (LoginForm) form;UserDao studentDao = new UserDao();String username = lf.getUserName();String password = lf.getPassword();int result = studentDao.queryUser(username, password);// 业务调用if (1 <= result) {// 用户名密码验证成功,跳转到成功页面return mapping.findForward("success");} else {// 用户名密码错误,跳转到失败页面return mapping.findForward("failing");}}}
?
运行结果和第三个版本一致!
?
源代码:
?