废话不多一切尽在代码中 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; } }
Java JDBC 帮助类 以及 获取连接
最新推荐文章于 2021-02-17 02:13:29 发布