Velocity+Struts+Jdbc+Mysql

最终效果: (注:第一幅图需要修改源码中的VM文件为以下代码)

Velocity+Struts+Jdbc+Mysql_第1张图片

Velocity+Struts+Jdbc+Mysql_第2张图片

Struts+Velocity整合示例,请访问:
http://qiaolevip.iteye.com/blog/1276857


sql:
CREATE TABLE `User` (
`id`  int NOT NULL ,
`name`  varchar(50) NULL 
)
;



DBHelper:
package com.ape.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DBHelper {
	public static Connection getConn() {
		Read read = new Read();
		Connection conn = null;
		try {
			Class.forName(read.readProperties("driver")).newInstance();
			conn = DriverManager.getConnection(read.readProperties("url"), read
					.readProperties("username"), read
					.readProperties("password"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return conn;
	}

	public static void closeAll(Connection conn, PreparedStatement ps) {
		try {
			if (conn != null) {
				conn.close();
			}
			if (ps != null) {
				ps.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static void closeAll(Connection conn, PreparedStatement ps,
			ResultSet rs) {
		try {
			if (conn != null) {
				conn.close();
			}
			if (ps != null) {
				ps.close();
			}
			if (rs != null) {
				rs.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}


VM
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
	li{
		font-size: 20px;
		color: green;
		text-align: left;
	}
	lable{
		color: red;
	}
</style>
<li>$!name</li>
<table border="1">
	<tr>
#foreach($user in $vector)
		<td>
            <li><lable>$velocityCount</lable>: $!user.id : $!user.name</li>
    	</td>
	#if($velocityCount % 3 == 0)
        </tr>
	#end
#end
</table>


public String test() {
		try {
			vector = service.retrieve();
			name = "Happy";
		} catch (ResourceNotFoundException e) {
			e.printStackTrace();
		} catch (ParseErrorException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "test";
	}

你可能感兴趣的:(velocity,乔乐共享)