主要功能是通过Struts2框架使用JDBC来访问MySQL数据库。
step1、ubuntu安装MySQL
1、安装
sudo apt-get update
sudo apt-get install mysql-server
2、启动和关闭mysql服务器:
service mysql start
service mysql stop
3、确认是否启动成功:
sudo netstat -tap | grep mysql
4、进入mysql shell界面:
mysql -u root -p
mysql中文乱码问题:
解决利用sqoop导入MySQL中文乱码的问题
导致导入时中文乱码的原因是character_set_server默认设置是latin1,如下图。
step2、创建数据库 命名为struts_tutorial,然后创建一个名为User的表,并填充一些值,下面是创建和填充脚本:
1、创建数据库
create database struts_tutorial;
2、创建表
create table User(
ID int(10) primary key not null,
uname varchar(10) not null,
upwd varchar(10) not null
);
3、插入值
insert into User(ID,uname,upwd) values('1','struts','longin');
step3、创建struts2工程
添加需要的jar文件和MySQL驱动jar。
创建好的工程目录结构如下:
1)创建Action
在action方法中,我们使用user和password参数来检查用户是否存在,如果存在,则在下一个界面中显示用户名。如果用户输入错误的信息,则会再次将其发送到登录界面。
下面是数据路连接类:SQLCon.java
package test.struts;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class SQLCon {
// 连接实例
private static Connection conn = null;
//连接地址
String url = "jdbc:mysql://localhost:3306/struts_tutorial";
// MySQL用户名
String user = "root";
// MySQL密码
String password = "******";
public SQLCon() throws Exception {
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection(url,user,password);
}
//获得连接对象
public static Connection getConnection(){
return conn;
}
//关闭连接
public static void CloseCon() throws SQLException{
conn.close();
}
}
LoginAction类LoginAction.java
import java.sql.ResultSet;
import java.sql.SQLException;
public class LoginAction {
private String name;
private String password;
private String result = "error";
public String execute() throws SQLException {
// if("struts".equals(name) && "login".equals(password))
// return "success";
// else
// return "error";
// 新建连接
try {
new SQLCon();
} catch (Exception e) {
// TODO: handle exception
result = "error";
}
// SQL语句
String sql = "select * from User where uname ='" + name + "' and upwd='" + password + "'";
// 获取查询结果
try {
ResultSet rs = SQLCon.getConnection().createStatement().executeQuery(sql);
if (rs.next()) {
result = "success";
}
} catch (SQLException e) {
// TODO: handle exception
result = "error";
} finally {
// TODO: handle finally clause
SQLCon.CloseCon();
}
return result;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
2)创建视图
包括三个jsp文件,分别为:index.jsp, success.jsp, error.jsp
1、index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h1>Hello World From Struts2</h1>
<form action="loginaction">
<label for="name">Please enter your name</label><br/>
<input type="text" name="name"/><br/>
<input type="text" name="password"/><br/>
<input type="submit" value="Say Hello"/>
</form>
</body>
</html>
2、success.jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Successful Login</title>
</head>
<body>
Hello World, <s:property value="name"/>,<s:property value="password"/>
</body>
</html>
3、error.jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Invalid User Name or Password</title>
</head>
<body>
Wrong user name or password provided.
</body>
</html>
3)配置文件
1、struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="helloworld" extends="struts-default">
<action name="loginaction"
class="test.struts.LoginAction"
method="execute">
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
2、web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>Struts 2</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
step4、功能测试
至此struts2访问数据库测试用例已经写完,现在,右键单击项目名称,然后单击“Export”> “WAR File”以创建WAR文件。然后在Tomcat的webapps目录中部署WAR文件。最后,启动Tomcat服务器并尝试访问URL http://localhost:8080/TestStruts/index.jsp,将显示以下界面:
登录成功后界面
登录失败后界面