Java JDBC 帮助类 以及 获取连接

1 篇文章 0 订阅
1 篇文章 0 订阅



废话不多一切尽在代码中


import java.sql.*;

/**
 * 
 * JDBC 帮助类 可根据自身情况添加 修改
 * 
 * @author Aire Yang
 * 
 * @date 2012-8-19
 * 
 * @description a SqlHelper 
 *
 */
public class SqlHelper {
	
	/**
	 * private constructor
	 */
	private SqlHelper(){
		
	}
	
	/**
	 * 执行Sql命令返回结果集
	 * @param connection 连接
	 * @param sql 查询语句
	 * @param objects 可变长参数
	 * @return 返回结果集
	 * @throws SQLException 遇到错误抛出异常
	 */
	public static ResultSet executeQuery(Connection connection,String sql,Object...objects)throws SQLException{
		
		return getPreparedStatement(connection, sql, objects).executeQuery();
	}
	
	/**
	 * 执行 插入,删除, 更新等  返回受影响行数
	 * @param connection 连接
	 * @param sql 插入,删除, 更新等 命令
	 * @param objects 可变参数
	 * @return 返回影响行数
	 * @throws SQLException 遇到错误抛出异常
	 */
	public static int executeUpdate(Connection connection,String sql,Object...objects)throws SQLException{
		
		return getPreparedStatement(connection, sql, objects).executeUpdate();
	}
	
	/**
	 * 得到preparedStatement对象
	 * @param connection 连接
	 * @param sql DDL 语句
	 * @param objects 可变参数 (可变参数)
	 * @return 返回preparedStatement对象
	 * @throws SQLException 遇到错误抛出异常
	 */
	public static PreparedStatement getPreparedStatement(Connection connection,String sql,Object...objects)throws SQLException{
		
		PreparedStatement preparedStatement=connection.prepareStatement(sql);
		attachParameters(preparedStatement, objects);
		
		return preparedStatement;
	}
	
	/**
	 * 得到CallableStatement对象
	 * @param connection 连接
	 * @param sql DDL 语句
	 * @param objects 可变参数 (可变参数)
	 * @return 返回preparedStatement对象
	 * @throws SQLException 遇到错误抛出异常
	 */
	public static CallableStatement getCallableStatement(Connection connection,String sql,Object...objects)throws SQLException{
		
		CallableStatement callableStatement=connection.prepareCall(sql);
		attachParameters(callableStatement, objects);
		
		return callableStatement;
	}
	
	/**
	 * 将参数加到PrepareStatemetePrepareStatemete或CallableStatement对象中
	 * @param preparedStatement  要添加参数的PrepareStatemete或CallableStatement对象
	 * @param objects 可变参数
	 * @throws SQLException 遇到错误抛出异常
	 */
	private static void attachParameters(PreparedStatement preparedStatement,Object...objects)throws SQLException{
		
		for(int i=0;i<objects.length;i++){
			preparedStatement.setObject(i+1, objects[i]);
		}
	}
	
	/**
	 * 用作对传入参数的检查
	 * @param connection 检查Connection 对象
	 * @param sql 检查SQl语句
	 * @throws SQLException 遇到错误抛出异常
	 */
	private static void chickParameters(Connection connection,String sql) throws SQLException{
		if(connection==null) throw new SQLException("connection 对象为空");
		if(sql.endsWith("")||sql==null) throw  new SQLException("查询语句错误"+sql);
	}
	
}


import java.sql.*;

/**
 * 获取JDBC 连接类 可根据自身情况添加 
 * 
 * @author Aire Yang
 * 
 * @date 2012-8-19
 * 
 * @description a DBconnection 
 *
 */
public class DBConnection {
	
	/**
	 * 驱动
	 */
	private static final String driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
	
	/**
	 * URL
	 */
	private static String url="jdbc:sqlserver://127.0.0.1:1433;databaseName=Northwind";
	
	/**
	 * 用户名 
	 */
	private static String uid="sa";
	
	/**
	 * 密码
	 */
	private static String pwd="111111";
	
	
	/**
	 * 加载驱动
	 */
	static {
		try {
			Class.forName(driver);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private  DBConnection() {
		// TODO Auto-generated constructor stub
	}
	
	
	/**
	 * 获取连接对象
	 * @return 连接对象
	 */
	public static Connection getConnection(){	
		try {
			return DriverManager.getConnection(url,uid,pwd);
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	} 
}


















































评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值