JAVA学习心得——DBUtil工具类

接口:

package com.jd.util;
import java.sql.ResultSet;
	public interface IRowMapper{
		void rowMapper(ResultSet result);
	}

工具类:
提供防止SQL注入和不防止SQL注入两种方法:

package com.jd.util;

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

/**
 * 数据库工具
 * 
 * @author 冯申然
 */
public class DBUtil {
	
	/**
	 * 程序驱动
	 * 
	 * @author 冯申然
	 */
	static {
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 连接mysql数据库
	 * 
	 * @author 冯申然
	 */
	public static  Connection  getconnection() {
		try {
			return  DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test",root, root);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;	
	}
	
	/**
	 * 数据的增删改查
	 * 
	 * @author 冯申然
	 */
		public static boolean updata(String sql) {
			Connection connection =null;
			Statement statement =null;
			try {
				connection=getconnection();
				statement = connection.createStatement();
				int result = statement.executeUpdate(sql);
				return result>0;
			} catch (Exception e) {
				e.printStackTrace();
			}finally {
				close(statement,connection);
			}
			return false;
		}
		
		/**
		 * 数据增删改查
		 * @author 冯申然
		 */
		public static boolean updata(String sql,Object...params) {
			Connection connection =null;
			PreparedStatement prepareStatement=null;
			try {
				connection=getconnection();
				prepareStatement = connection.prepareStatement(sql);
				
				for(int i=1;i<=params.length;i++) {
					prepareStatement.setObject(i, params[i-1]);
				}
				int result = prepareStatement.executeUpdate();
				return result>0;
			} catch (Exception e) {
				e.printStackTrace();
			}finally {
				close(prepareStatement,connection);
			}
			return false;
		}
		
		/**
		 * 登录设置
		 * 
		 * @author 冯申然
		 */
		public static void select(String sql,IRowMapper rowMapper,Object...params) {//登录设置
			ResultSet result =null;
			Statement statement =null;
			Connection connection =null;
			PreparedStatement preparedStatement =null;
			try {
				connection=getconnection();
				preparedStatement =connection.prepareStatement(sql);
				for(int i=1;i<=params.length;i++) {
					preparedStatement.setObject(i, params[i-1]);
				}
				result =preparedStatement.executeQuery();
				rowMapper.rowMapper(result);
			} catch (Exception e) {
				e.printStackTrace();
			}finally {
				close(result,statement,connection);
			}
		}
		
		/**
		 * 检验数据是否存在
		 * 
		 * @author 冯申然
		 */
		public static boolean exist (String sql) {//是否存在
			class RowMapper implements IRowMapper{
				boolean state;
				@Override
				public void rowMapper(ResultSet result) {
					try {
						state=result.next();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}		
			}
			RowMapper rowMapper =new RowMapper();
			select(sql,rowMapper);
			return rowMapper.state;
		}
		
		/**
		 * 检验数据是否存在
		 * 
		 * @author 冯申然
		 */
		public static boolean exist (String sql,Object...params) {//是否存在
			class RowMapper implements IRowMapper{
				boolean state;
				@Override
				public void rowMapper(ResultSet result) {
					try {
						state=result.next();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}		
			}
			RowMapper rowMapper =new RowMapper();
			select(sql,rowMapper,params);
			return rowMapper.state;
		}
		
		/**
		 * 查询数据
		 * 
		 * @author 冯申然
		 */
		public static void select(String sql,IRowMapper rowMapper) {//查询
				Connection connection =null;
				Statement statement =null;
				ResultSet result =null;
				try {
					connection=getconnection();
					statement = connection.createStatement();
					result = statement.executeQuery(sql);
					rowMapper.rowMapper(result);
				} catch (Exception e) {
					e.printStackTrace();
				}finally {
					close(result,statement,connection);
				}	
			}
		
		/**
		 * 查询数据
		 * 
		 * @author 冯申然
		 */
		public static void Select(String sql,IRowMapper rowMapper,Object...params) {//查询
			Connection connection =null;
			Statement statement =null;
			ResultSet result =null;
			PreparedStatement preparedStatement =null;
			try {
				connection=getconnection();
				preparedStatement =connection.prepareStatement(sql);
				for(int i=1;i<=params.length;i++) {
					preparedStatement.setObject(i, params[i-1]);
				}
				result =preparedStatement.executeQuery() ;
				rowMapper.rowMapper(result);
			} catch (Exception e) {
				e.printStackTrace();
			}finally {
				close(result,statement,connection);
			}	
		}
		
		/**
		 * 释放资源
		 * 
		 * @author 冯申然
		 */
		private static void close(Statement statement,Connection connection){
				
			if (statement!=null) {
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				} 
			}
			if (connection!=null) {
				try {
					connection.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}	
				}
			}
			private static void close(ResultSet result,Statement statement,Connection connection){
				if (result!=null) {
					try {
						result.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
				if (statement!=null) {
					try {
						statement.close();
					} catch (SQLException e) {
						e.printStackTrace();
					} 
				}
				if (connection!=null) {
						try {
							connection.close();
						} catch (SQLException e) {
							e.printStackTrace();
						} 		
				}
			}
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值