servlet(CRUD)以飨初学者

用业余时间写成的使用servlet的一个DEMO,(希望对初学者有所帮助,末了我会把完整的程序代码都贴出来)


先简要回顾一下servlet的使用(具体的请参考servlet的api[url]http://tomcat.apache.org/tomcat-5.5-doc/servletapi/index.html[/url],或者百度一下 :D ), 首先是类层次结构,我们自己的servlet一般都继承自httpservlet, httpservlet继承自genericservlet, genericservlet实现了servlet(声明了init 、service。。等方法)和servletconfig(声明了getServletName、 getservletContext。等方法)接口。
[img]http://www.iteye.com/upload/attachment/30839/82650e2b-69a6-3542-9896-82f35bdc5298.jpg[/img]


getParameter :以字符串的形式返回请求参数的值,如果请求参数不存在,返回null,请求参数作为额外的信息和请求一起被发送。对于httpservlet来说,请求参数被存放与查询字符串或post表单中。


setAttribute :在本次请求中存放一个属性, 多个请求之间属性会被复位。


getRequestDispatcher :返回一个用于包装路径的类。

forward :转发请求到另一个资源(servlet, JSP file, or HTML file)

include :包含一个资源的上下文。
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

下面是有关jdbc操作的辅助类(代码不足之处,请多多指正。)
DButil

package tutorial;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Arrays;

/**
* 数据库操作工具类
*
* @author cong-px
*/
public class DButil {

private static DButil instance;

/*
* private 构造方法
*/
private DButil() {
}

/**
* 单例
*
* @return
*/
public synchronized static DButil getInstance() {
if (null == instance) {
instance = new DButil();
}
return instance;
}

/**
* 打开连接
*
* @return conn 连接
* @throws ClassNotFoundException
* @throws SQLException
*/
public Connection getConnection() throws ClassNotFoundException,
SQLException {
// 加载类
Class.forName("com.mysql.jdbc.Driver");

// H2Database 连接数据库
// Class.forName("org.h2.Driver");
// 获得连接
Connection conn = DriverManager
.getConnection("jdbc:mysql://localhost:3306/servletapp?user=root&password=123456&&useUnicode=true&characterEncoding=utf-8");
// Connection conn = DriverManager.getConnection("jdbc:h2:~/test", "sa", "");
// 返回连接
return conn;
}

/**
* 关闭连接
*
* @throws SQLException
*/
public static void closeConnection(Connection conn) throws SQLException {
if (null != conn) {
conn.close();
}
}

/**
* 获得PreparedStatement
*
* @param sql
* @return
* @throws SQLException
*/
public PreparedStatement getPreparedStatement(Connection conn, String sql)
throws SQLException {
PreparedStatement preparedStatement = conn.prepareStatement(sql);
return preparedStatement;
}

/**
* 关闭 PreparedStatement
*
* @throws SQLException
*/
public static void closePreparedStatement(
PreparedStatement preparedStatement) throws SQLException {
if (null != preparedStatement) {
preparedStatement.close();
}
}

/**
* 关闭ResultSet
*
* @throws SQLException
*/
public static void closeResultSet(ResultSet resultSet) throws SQLException {
if (null != resultSet) {
resultSet.close();
}
}

/**
* 查询操作<B>方法后不要忘记关闭连接</B>
*
* @param sql
* @param params
* @param preparedStatement
* @return resultSet 结果集
* @throws SQLException
* @throws ClassNotFoundException
*/
public ResultSet ExecuteQuery(PreparedStatement preparedStatement,
String sql, Object[] params) throws SQLException,
ClassNotFoundException {
ResultSet resultSet = null;
try {
this.fillStatement(preparedStatement, params);
resultSet = preparedStatement.executeQuery();
} catch (SQLException e) {
this.rethrow(e, sql, params);
}
return resultSet;
}

/**
* 更新操作
*
* @param sql
* @param params
* @return rows 更新条数
* @throws SQLException
* @throws ClassNotFoundException
*/
public int ExecuteUpdate(String sql, Object[] params) throws SQLException,
ClassNotFoundException {
// 影响的行数
int rows = 0;
PreparedStatement preparedStatement = null;
Connection conn = null;
try {
conn = this.getConnection();
preparedStatement = this.getPreparedStatement(conn, sql);
this.fillStatement(preparedStatement, params);
rows = preparedStatement.executeUpdate();
} catch (SQLException e) {
this.rethrow(e, sql, params);
} finally {
DButil.closePreparedStatement(preparedStatement);
DButil.closeConnection(conn);
}
return rows;
}

/**
* 参数化preparedStatement
*
* @param stmt
* @param params
* @throws SQLException
*/
protected void fillStatement(PreparedStatement stmt, Object[] params)
throws SQLException {
if (null == params) {
return;
}
/*
* 参数化preparedStatement
*/
for (int i = 0; i < params.length; i++) {
if (null != params[i]) {
stmt.setObject(i + 1, params[i]);
} else {
stmt.setNull(i + 1, Types.CHAR);
}
}
}

/**
* 重新抛出异常
*
* @param cause
* @param sql
* @param params
* @throws SQLException
*/
protected void rethrow(SQLException cause, String sql, Object params[])
throws SQLException {
StringBuilder msg = new StringBuilder();
msg.append(" Query :");
msg.append(sql);
if (null == params) {
msg.append("[]");
} else {
msg.append(Arrays.asList(params));
}
SQLException exception = new SQLException(msg.toString(), cause
.getSQLState(), cause.getErrorCode());

//
exception.setNextException(cause);
throw exception;
}

/**
* 不对异常进行处理
*
* @param conn
*/
public static void closeQuietly(Connection conn) {
try {
DButil.closeConnection(conn);
} catch (Exception e) {
// quiet
}
}

/**
* 不对异常进行处理
*
* @param preparedStatement
*/
public static void closeQuietly(PreparedStatement preparedStatement) {
try {
DButil.closePreparedStatement(preparedStatement);
} catch (Exception e) {
// quiet
}
}

/**
* 不对异常进行处理
*
* @param resultSet
*/
public static void colseQuietly(ResultSet resultSet) {
try {
resultSet.close();
} catch (Exception e) {
// quiet
}
}

/**
* 不对异常进行处理
*
* @param conn
* @param preparedStatement
* @param resultSet
*/
public static void colseQuietly(Connection conn,
PreparedStatement preparedStatement, ResultSet resultSet) {
try {
DButil.colseQuietly(resultSet);
} finally {
try {
DButil.closeQuietly(preparedStatement);
} finally {
DButil.closeQuietly(conn);
}
}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值