标签:ext logs border sch blog erp getc dynamic size
这是个音乐播放列表
1、queryMusic.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib prefix="s" uri="/struts-tags" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>Music List</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <table border="1" cellpadding="0" cellspacing="0" width="700" height="300"> <tr height="200"> <td align="center">序号</td> <td align="center">歌曲名</td> <td align="center">歌手名</td> <td align="center">歌曲大小</td> <td align="center">试听</td> <td align="center">下载</td> <td align="center">操作</td> </tr> <s:iterator var="music" value="musicList" status="no"> <tr> <td align="center">${no.count}</td> <td align="center">${music.musicName}</td> <td align="center">${music.singer}</td> <td align="center">${music.size}</td> <td align="center"><img src="image/st.png" /></td> <td align="center"><img src="image/xz.png" /></td> <td align="center"><a href="#">删除</a></td> </tr> </s:iterator> </table> </body> </html>
2、Music.java
package com.cn.pojo; public class Music { private String musicName; private String singer; private String size; public String getMusicName() { return musicName; } public void setMusicName(String musicName) { this.musicName = musicName; } public String getSinger() { return singer; } public void setSinger(String singer) { this.singer = singer; } public String getSize() { return size; } public void setSize(String size) { this.size = size; } public Music() { super(); } public Music(String musicName, String singer, String size) { super(); this.musicName = musicName; this.singer = singer; this.size = size; } }
3、DBHelper.java
package com.cn.tools; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; /** * 获取数据库操作的连接对象 * 关闭数据库操作的各种资源 * @author 晏先政 * */ public class DBHelper { private static final String className = "com.mysql.jdbc.Driver"; private static final String url = "jdbc:mysql://localhost:3306/tlx?characterEncoding=utf8&useSSL=true"; private static final String uname = "root"; private static final String upass = "211599100yxz"; /** * 获取数据库连接对象的方法 */ public static Connection getConn(){ Connection conn = null; try{ Class.forName(className); conn = DriverManager.getConnection(url,uname, upass); } catch(Exception e){ e.printStackTrace(); } return conn; } /** * 关闭数据库连接对象 */ public static void closeConn(Connection conn){ try{ if(conn!=null){ conn.close(); } } catch(Exception e){ e.printStackTrace(); } } /** * 关闭数据库操作对象 */ public static void closeStmt(Statement stmt){ try{ if(stmt!=null){ stmt.close(); } } catch(Exception e){ e.printStackTrace(); } } /** * 关闭数据库操作对象 */ public static void closePstmt(PreparedStatement pstmt){ try{ if(pstmt!=null){ pstmt.close(); } } catch(Exception e){ e.printStackTrace(); } } /** * 关闭数据库操作对象 */ public static void closeRs(ResultSet rs){ try{ if(rs!=null){ rs.close(); } } catch(Exception e){ e.printStackTrace(); } } }
4、MusicDao.java
package com.cn.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; import java.util.Map; import javax.servlet.jsp.jstl.sql.Result; import javax.servlet.jsp.jstl.sql.ResultSupport; import com.cn.pojo.Music; import com.cn.tools.DBHelper; public class MusicDao { private Connection conn = null; private PreparedStatement pstmt = null; private ResultSet rs = null; public void addMusic(Music music){ try { conn = DBHelper.getConn(); String sql = "insert into music_info(musicName,singer,size) values(?,?,?)"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1, music.getMusicName()); pstmt.setString(2, music.getSinger()); pstmt.setString(3, music.getSize()); pstmt.execute(); } catch (Exception e) { e.printStackTrace(); }finally{ DBHelper.closePstmt(pstmt); DBHelper.closeConn(conn); } } public List<Music> queryMusic() { List<Music> musicList = new ArrayList<Music>(); // Music music = new Music(); try { conn = DBHelper.getConn(); String sql = "select * from music_info"; pstmt = conn.prepareStatement(sql); rs = pstmt.executeQuery(); Result rst = ResultSupport.toResult(rs); Map[] rows = rst.getRows(); for(Map row:rows){ String musicName = row.get("musicName").toString(); String singer = row.get("singer").toString(); String size = row.get("size").toString(); Music music = new Music(musicName,singer,size); musicList.add(music); System.out.println(musicList); } } catch (Exception e) { e.printStackTrace(); }finally{ DBHelper.closeRs(rs); DBHelper.closePstmt(pstmt); DBHelper.closeConn(conn); } return musicList; } public void deleteMusic(){ } public void updateMusic(Music music) { // TODO Auto-generated method stub } }
5、MusicManagerAction.java
package com.cn.action; import java.util.List; import com.cn.dao.MusicDao; import com.cn.pojo.Music; public class MusicManagerAction { private List<Music> musicList; private MusicDao musicDao = new MusicDao(); private Music music; public String queryMusic(){ musicList = musicDao.queryMusic(); System.out.println(musicList); return "queryMusic"; } public String deleteMusic(){ musicDao.deleteMusic(); musicList = musicDao.queryMusic(); return "deletemusic"; } public String addMusic(){ musicDao.addMusic(music); return "addMusic"; } public String updateMusic(){ musicDao.updateMusic(music); return "updatemusic"; } public List<Music> getMusicList() { return musicList; } public void setMusicList(List<Music> musicList) { this.musicList = musicList; } }
6、struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.action.extension" value="do"></constant> <!-- 实现对动态调用的支持 --> <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant> <package name="test" namespace="/" extends="struts-default"> <action name="labelAction_*" class="com.cn.action.LabelAction" method="{1}"> <result name="{1}">/{1}.jsp</result> </action> <action name="MusicManagerAction" class="com.cn.action.MusicManagerAction"> <result name="queryMusic">/queryMusic.jsp</result> <result name="deleteMusic">/queryMusic.jsp</result> <result name="addMusic" type="redirectAction"> <param name="actionName">MusicManagerAction</param> <param name="method">queryMusic</param> </result> <result name="updateMusic" type="redirectAction"> <param name="actionName">MusicManagerAction</param> <param name="method">queryMusic</param> </result> </action> <action name="UserManagerAction_*" class="com.cn.action.UserManagerAction" method="{1}"> <result name="{1}">/register.jsp</result> </action> </package> </struts>
标签:ext logs border sch blog erp getc dynamic size
原文地址:http://www.cnblogs.com/Vito-Yan/p/7429497.html