简洁版JDBCUtil工具类(基于反射)-实现简单CRUD操做

JDBCUtil工具类

1.  定义静态成员变量

 2. 静态代码块加载JDBCUtil资源(须要配置db.properties配置文件)

/**
	 * 加载JDBCUtil资源文件       
	 */
	static {
		try {
			InputStream inputStream=JDBCUtil.class.getClassLoader().getResourceAsStream("db.properties");
			properties.load(inputStream);
			inputStream.close();
			Class.forName(properties.get("driverName").toString());
		} catch (IOException e) {
			
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

 3. 获取Connection链接对象

/**
	 * 获取数据库链接对象
	 * @return
	 */
	public static Connection getConnect()
	{
		Connection connection=null;
		try {
			connection=DriverManager.getConnection(properties.getProperty("url"),
          properties.getProperty("user"),properties.getProperty("password"));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return connection;
	}

4. 释放数据库链接对象资源

/**
	 * 释放资源
	 * @param resultSet
	 * @param preparedStatement
	 * @param connection
	 */
	
	public static void Close(ResultSet resultSet,PreparedStatement preparedStatement,Connection connection)
	{
		try {
			if(resultSet!=null)
			{
				resultSet.close();
			}
			if(preparedStatement!=null)
			{
				preparedStatement.close();
			}
			if(connection!=null)
			{
				connection.close();
			}
		}catch (SQLException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
	}

 5. 增删改操做

/**
	 * 增删改
	 * @param sql      Sql语句
	 * @param object    Sql参数
	 * @return
	 */
	public static int executeUpdateOrInsertOrDel(String sql,Object object [])
	{
		int result=-1;
		Connection connection=getConnect();
		PreparedStatement preparedStatement;
		try {
			
			preparedStatement=connection.prepareStatement(sql);
			if(object.length>0)
			{
				for(int i=0;i<object.length;i++)
				{
					preparedStatement.setObject(i+1, i);
				}
			}
			result=preparedStatement.executeUpdate();
			Close(null, preparedStatement, connection);
		} catch (SQLException e) {
			
			e.printStackTrace();
		}	
		return result;
	}

6. 查询操做 (操做数据封装到List列表)

/**
	 * 查询
	 * @param sql  Sql语句
	 * @param cla  封装数据类型
	 * @param objects  Sql语句参数
	 * @return
	 * @throws Exception
	 */
	public static List<Object> executeQuery(String sql,Class<?> cla,Object [] objects) throws Exception
	{
		List<Object> list=new ArrayList<>();
		
		Connection connection=getConnect();
		
		PreparedStatement preparedStatement=connection.prepareStatement(sql);
		
		if(objects.length>0)
		{
			for(int i=0;i<objects.length;i++)
			{
				preparedStatement.setObject(i+1, objects[i]);
			}
		}
		
		ResultSet resultSet=preparedStatement.executeQuery();
		
		Class<?> clz=Class.forName(cla.getName());
		while(resultSet.next())
		{
			Object bj=clz.newInstance();
			Field [] fdFields=clz.getDeclaredFields();
			
			for(Field f:fdFields)
			{
				f.setAccessible(true);
				
				String typeValue=f.getType().getName();
				
				Object beforeValue=resultSet.getObject(f.getName());
				
				Object afterValue=null;
				
				switch (typeValue) {
					case "int":
					case "java.lang.Integer":
						if(beforeValue!=null)
						{
							afterValue=Integer.parseInt(beforeValue.toString());
						}else {
							afterValue=0;
						}
						break;
					case "double":
					case "java.lang.Double":
						if(beforeValue!=null)
						{
							afterValue=Double.parseDouble(beforeValue.toString());
						}else {
							afterValue=0d;
						}
						break;
					case "float":
					case "java.lang.Float":
						if(beforeValue!=null)
						{
							afterValue=Float.parseFloat(beforeValue.toString());
						}else {
							afterValue=0f;
						}
	
					default:
						afterValue=beforeValue;
						break;
					}
				f.set(bj, afterValue);
				
				
			}
			list.add(bj);
		}
		Close(resultSet, preparedStatement, connection);
		return list;
	}