Velocity+Struts+Jdbc+Mysql

本文详细介绍了如何使用Struts+Velocity进行整合,并通过DBHelper实现数据库操作,包括创建表、获取连接、关闭资源等常用功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[size=xx-large]最终效果:[/size][color=red](注:第一幅图需要修改源码中的VM文件为以下代码)[/color]

[img]http://dl.iteye.com/upload/attachment/595447/0d215fb8-31c7-3e72-be70-ab61640dcce4.jpg[/img]

[img]http://dl.iteye.com/upload/attachment/595426/ba1fa811-93c7-3167-8b3c-2316423a8d0b.png[/img]

[size=large]Struts+Velocity整合示例,请访问:[/size]
[url]http://qiaolevip.iteye.com/blog/1276857[/url]


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



[size=xx-large]DBHelper:[/size]
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";
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值