</pre> 现在 ,接着上一章的内容来说,这一张主要是给大家说下我搭建服务器的时候常用的基本代码,这样就不要到时候一步一步的写了。<p></p><p> <img src="https://img-blog.csdn.net/20160123185009227?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="" />这个是当初写项目的时候一个架构,现在,我给出几个重要文件的源代码,以供大家参考。</p><p></p><p></p><p></p><p></p><p> SQLHelper类:</p><p></p><pre name="code" class="java">package com.cars.helper;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
public class SQLHelper {
/*
* 驱动
*/
public static String driver="com.mysql.jdbc.Driver";
/*
* 连接字符串 并且带上数据库名称
*/
public static String url ="jdbc:mysql://localhost:3306/test1";
/*
* 用户名
*/
public static String user ="root";
/*
* 密码
*/
public static String pwd="" ;
// static{
//
// Properties properties=new Properties();
// try {
// InputStream inputStream=ConnectionFactory.class.getClassLoader().getResourceAsStream("dbconfig.properties");
//
// properties.load(inputStream);
// } catch (IOException e) {
//
// System.out.println("------配置文件读取错误-----");
// e.printStackTrace();
// }
// driver=properties.getProperty("driver");
// url=properties.getProperty("dburl");
// user=properties.getProperty("user");
// pwd=properties.getProperty("paw");
// }
// 不允许实例化该类
private SQLHelper() {
}
/*
* 获取一个数据库的链接 通过driver/url/user/pwd这四个静态变量来进行设置数据库的连接属性
*/
public static Connection getConnection() {
try {
Class.forName(driver);
return DriverManager.getConnection(url, user, pwd);
} catch (Exception e) {
// TODO: handle exception
Logger.getLogger(SQLHelper.class.getName()).log(Level.SEVERE, null, e);
System.out.println("链接出错");
return null;
}
}
/*
* 获取一个Statement
*/
public static Statement getStatment() {
Connection connection = getConnection();
if (connection == null) {
return null;
}
try {
return connection.createStatement();
} catch (Exception e) {
// TODO: handle exception
System.out.println("connection出错");
return null;
}
}
/*
* 获取一个带参数的 PreparedStatement 该 PreparedStatement 已经设置数据集 可以滚动,可以更新
*
* @param sql 需要 ? 参数的 SQL 语句
*
* @param cmdParmas SQL 语句的参数表
*
* @return 如果获取失败将返回 null,调用时记得检查返回值
*
* 这个可以用来处理事务
*/
public static PreparedStatement getPreparedStatment(Connection con,String sql, Object... cmdParmas) {
Connection connection = con;
if (connection == null) {
return null;
}
PreparedStatement preparedStatement = null;
try {
preparedStatement = (PreparedStatement) connection.prepareStatement(sql);
int i = 1;
for (Object item : cmdParmas)
{
preparedStatement.setObject(i, item);
i++;
}
return preparedStatement;
} catch (Exception e) {
// TODO: handle exception
System.out.println("PreparedStatement出错");
return null;
}
}
//不用执行事务的时候调用
public static PreparedStatement getPreparedStatment(String sql, Object... cmdParmas) {
Connection connection = getConnection();
if (connection == null) {
return null;
}
PreparedStatement preparedStatement = null;
try {
preparedStatement = (PreparedStatement) connection.prepareStatement(sql);
int i = 1;
for (Object item : cmdParmas)
{
preparedStatement.setObject(i, item);
i++;
}
return preparedStatement;
} catch (Exception e) {
// TODO: handle exception
System.out.println("PreparedStatement出错");
return null;
}
}
/**
* 执行 SQL 语句,返回结果为整型 主要用于执行非查询语句
*
* @param cmdText
* 需要 ? 参数的 SQL 语句
* @param cmdParams
* SQL 语句的参数表
* @return 非负数:正常执行; -1:执行错误; -2:连接错误
* @throws SQLException
*用来执行事务的时候调用
*/
public static int ExeSql(Connection con,String sql, Object... cmdParams) throws SQLException {
PreparedStatement preparedStatement = getPreparedStatment(con,sql, cmdParams);
if (preparedStatement == null) {
return -2;
}
int i = 0;
i = preparedStatement.executeUpdate();
close(preparedStatement);
return i;
}
//不用来执行事务的时候
public static int ExeSql(String sql, Object... cmdParams) throws SQLException {
PreparedStatement preparedStatement = getPreparedStatment(sql, cmdParams);
if (preparedStatement == null) {
return -2;
}
int i = 0;
i = preparedStatement.executeUpdate();
close(preparedStatement);
return i;
}
/**
* 返回结果集的第一行的一列的值,其他忽略
*
* @param cmdText
* 需要 ? 参数的 SQL 语句
* @param cmdParams
* SQL 语句的参数表
* @return
*/
public static Object ExecScalar(String cmdText, Object... cmdParams) {
ResultSet rs = getResultSet(cmdText, cmdParams);
Object obj = buildScalar(rs);
closeConnection(rs);
return obj;
}
/**
* 返回一个 ResultSet
*
* @param cmdText
* 需要 ? 参数的 SQL 语句
* @param cmdParams
* SQL 语句的参数表
* @return
*/
public static ResultSet getResultSet(String cmdText, Object... cmdParams) {
PreparedStatement pstmt = getPreparedStatment(cmdText, cmdParams);
if (pstmt == null) {
return null;
}
try {
return pstmt.executeQuery();
} catch (SQLException ex) {
Logger.getLogger(SQLHelper.class.getName()).log(Level.SEVERE, null, ex);
closeConnection(pstmt);
}
return null;
}
/**
* 返回结果集中的第一条第一个数据
*
* @param rs
* @return
*/
public static Object buildScalar(ResultSet rs) {
if (rs == null) {
return null;
}
Object obj = null;
try {
if (rs.next()) {
obj = rs.getObject(1);
}
} catch (SQLException ex) {
Logger.getLogger(SQLHelper.class.getName()).log(Level.SEVERE, null, ex);
}
return obj;
}
/**
* 关闭链接或者数据库
*
* @param obj
*/
private static void closeConnection(Object obj) {
if (obj == null) {
return;
}
try {
if (obj instanceof Statement) {
((Statement) obj).getConnection().close();
} else if (obj instanceof PreparedStatement) {
((PreparedStatement) obj).getConnection().close();
} else if (obj instanceof ResultSet) {
((ResultSet) obj).getStatement().getConnection().close();
} else if (obj instanceof Connection) {
((Connection) obj).close();
}
} catch (SQLException ex) {
Logger.getLogger(SQLHelper.class.getName()).log(Level.SEVERE, null, ex);
}
}
private static void close(Object obj)
{
if (obj == null)
{
return;
}
try
{
if (obj instanceof Statement)
{
((Statement) obj).close();
} else if (obj instanceof PreparedStatement)
{
((PreparedStatement) obj).close();
} else if (obj instanceof ResultSet)
{
((ResultSet) obj).close();
} else if (obj instanceof Connection)
{
((Connection) obj).close();
}
} catch (SQLException ex)
{
Logger.getLogger(SQLHelper.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
charsetFiltet类:
package com.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
/**
* Servlet Filter implementation class CharacterFilter
*/
@WebFilter("/CharacterFilter")
public class CharacterFilter implements Filter {
/**
* Default constructor.
*/
public CharacterFilter() {
// TODO Auto-generated constructor stub
}
/**
* @see Filter#destroy()
*/
public void destroy() {
// TODO Auto-generated method stub
}
/**
* @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
chain.doFilter(request, response);
}
/**
* @see Filter#init(FilterConfig)
*/
public void init(FilterConfig fConfig) throws ServletException {
// TODO Auto-generated method stub
}
}
相对应的 config文件配置应该如下:
<servlet>
<servlet-name>City</servlet-name>
<servlet-class>com.cars.data.GetCities</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>City</servlet-name>
<url-pattern>/City</url-pattern>
</servlet-mapping>
<filter>
<filter-name>c</filter-name>
<filter-class>com.filter.CharacterFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>c</filter-name>
<url-pattern>*</url-pattern>
</filter-mapping>
当拷贝代码的时候根据自己的实际情况来用,重在理解。其中 servlet是在data那个包里面的 相当于接口 filter包主要是进行数据的编码,输出的数据不是乱码。目前能想到的是这几个重要的点了,后面会陆续补充上来的。