• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

mysql调整struts2开发实例

mysql 搞代码 7年前 (2018-06-06) 296次浏览 已收录 0个评论

mysql整合struts2开发实例
本文主要介绍mysql和struts2整合开发的小例子,
欢迎转载,请注明原文地址:http://fuchangle.iteye.com/admin/blogs/1740332
首先下载JDBC驱动,google一下就能找到
然后贴出sql数据库代码

init.sql

 DROP DATABASE IF EXISTS databaseWeb; --如果存在,则删除数据库databaseWeb CREATE DATABASE databaseWeb CHARACTER SET utf8; --创建数据库,使用utf8编码  USE databaseWeb; --切换到数据库databaseWeb set Names 'gbk'; --控制台使用gbk编码  DROP TABLE IF EXISTS tb_person; CREATE TABLE tb_person(  id INTEGER AUTO_INCREMENT COMMENT 'id',  name VARCHAR(45) COMMENT '姓名',  english_name VARCHAR(45) COMMENT '英文名',  age INTEGER UNSIGNED COMMENT '年龄',  sex VARCHAR(45) COMMENT '性别',  birthday DATE COMMENT '出生日期',  description TEXT COMMENT '备注',  create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP() COMMENT '创建时间',  PRIMARY KEY(id) );  INSERT INTO tb_person (name,english_name,age,sex,birthday,description) values('刘京华','Helloweenvsfei','25','男','1982-08-09','无备注');  INSERT INTO tb_person (name,english_name,age,sex,birthday,description) values('科特柯本','Kurt Cobain','27','男','1967-02-20','Nirvana');  INSERT INTO tb_person (name,english_name,age,sex,birthday,description) values('李四','Faye','31','女','1969-08-08','狮子座');  INSERT INTO tb_person (name,english_name,age,sex,birthday,description) values('张三','Foo Bar','18','女','2008-08-08',''); 

欢迎大家阅读《mysql调整struts2开发实例》,跪求各位点评,by 搞代码

加入struts2框架后,编写action代码
JDBCTest

 package com.jdbc.test;  import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List;  import com.opensymphony.xwork2.ActionSupport;  public class JDBCTest extends ActionSupport{   private static final long serialVersionUID = -6572397411780171140L;  public static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");  private List<PersonBean> list;  public static Connection getConnection() throws ClassNotFoundException, SQLException{   String url = "jdbc:mysql://localhost:3306/databaseWeb";   Class.forName("com.mysql.jdbc.Driver");   String username = "root";   String password = "admin";   Connection con = DriverManager.getConnection(url, username, password);   return con;  }    public String getPersonList() {   try {    Connection con = getConnection();    Statement sql = con.createStatement();    String query = "select * from tb_person";    ResultSet result = sql.executeQuery(query);    this.list = new ArrayList<PersonBean>();    while(result.next()){     PersonBean bean = new PersonBean();     bean.setId(String.valueOf(result.getInt("id")));     bean.setName(result.getString("name"));     bean.setEnglish_name(result.getString("english_name"));     bean.setAge(String.valueOf(result.getInt("age")));     bean.setSex(result.getString("sex"));     bean.setBirthday(dateFormat.format(result.getDate("birthday")));     list.add(bean);    }   } catch (Exception e) {    e.printStackTrace();   } finally{   }   return SUCCESS;  }   public List<PersonBean> getList() {   return list;  }   public void setList(List<PersonBean> list) {   this.list = list;  }   }  

封装数据使用Bean
PersonBean

 package com.jdbc.test;  public class PersonBean {    private String id;  private String name;  private String english_name;  private String age;  private String sex;  private String birthday;  public String getId() {   return id;  }  public void setId(String id) {   this.id = id;  }  public String getName() {   return name;  }  public void setName(String name) {   this.name = name;  }  public String getEnglish_name() {   return english_name;  }  public void setEnglish_name(String englishName) {   english_name = englishName;  }  public String getAge() {   return age;  }  public void setAge(String age) {   this.age = age;  }  public String getSex() {   return sex;  }  public void setSex(String sex) {   this.sex = sex;  }  public String getBirthday() {   return birthday;  }  public void setBirthday(String birthday) {   this.birthday = birthday;  }   }  

struts.xml配置

 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts>  <package name="main" extends="struts-default">      <action name="person_*" class="com.jdbc.test.JDBCTest" method="{1}">          <result name="success">/person_list.jsp</result>      </action>  </package> </struts>      

person_list.jsp

 <%@ page language="java" contentType="text/html; charset=utf-8"     pageEncoding="utf-8"%> <%@taglib uri="/struts-tags" prefix="s"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body>  <table border="1">   <tr>    <td>序号</td>    <td>姓名</td>    <td>英文名</td>    <td>年龄</td>    <td>性别</td>    <td>出生日期</td>   </tr>     <s:iterator value="list" var="item">   <tr>    <td><s:property value="#item.id"/></td>    <td><s:property value="#item.name"/></td>    <td><s:property value="#item.english_name"/></td>    <td><s:property value="#item.age"/></td>    <td><s:property value="#item.sex"/></td>    <td><s:property value="#item.birthday"/></td>   </tr>  </s:iterator>  </table>    </body> </html> 

好啦,就这么多,有遗漏的地方请留言,原项目见附件


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:mysql调整struts2开发实例

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址